I am developing a winform application in C# that connects to a mySQL database. It queries the DB returning to a datatable. Once the data is retrieved, it is put into a datagridview. My issue is the width of the datagridview is such that the columns do not fill it completely leaving grey space to the right of the the columns. Also, I would like to remove the grey space below the rows and fill with empty rows. I have googled and searched here but can't seem to find the answer to my issues. Any help would be appreciated. I can think of some crude ways to solve the problems, but I think there must be something more simple.
Thanks
Loading

EhteshamRank #3,979 · 374 reputationPosted May 22, 2012, 9:50 PM EDT
EhteshamRank #3,979 · 374 reputationPosted May 22, 2012, 2:59 AM EDT
For rows Fill is not available however you can use the below code
//use this code before binding
dataGridView1.RowTemplate.Height = this.dataGridView1.Height / TotalRows;
Where TotalRows is total number of rows which will be binded or added to datagridview
Shawn27 reputationPosted May 22, 2012, 6:03 AM EDT
As for the rows, it looks like that code would cause dynamic height of the rows depending on the number of rows. I would really like to keep the same row height, but fill in with blank rows for a more aesthetically pleasing look.
I really appreciate your response.
Shawn27 reputationPosted May 22, 2012, 9:44 PM EDT
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.DisplayedCells;
it was overriding the property set and the line of code I was putting in the constructor / load method (simply because it occurred last.) So now I have the problem so that if I remove the above line of code, the data is cutoff. Alternatively, the datagridview is not completely filled. So now I suspect I will need to stretch the column widths until the datagridview is filled. I was hoping to get around such a crude approach, but I don't see an option.
I suppose if it were easy we wouldn't have a job though right?
Anyway, I am still searching for a way to fill the datagridview vertically with empty rows without changing the row height.
Thanks again Ehtesham for your help.
Shawn27 reputationPosted May 23, 2012, 7:29 AM EDT
I still need to finalize my column widths so that it fills the dataGridView width. But, I'm on the right track and can make it happen. Thanks again and have a good day.
EhteshamRank #3,979 · 374 reputationPosted May 23, 2012, 8:47 PM EDT
karthik tirupathi2 reputationPosted Jun 7, 2014, 9:13 AM EDT
For me, your question seems like,you want to auto fill your datagridview with the rows and columns that you have.
Well, you can fill your datagridview according to number of columns by using below procedure...
1) Select your datagridview and go to properties window.
2) Change the AutoSizeColumnsMode to Fill.
For removing grey color below row of datagridview, we do not have inbuilt property...but we can fill the datagridview with empty rows by writing the following code in the Form_Load event for the form containing the datagridview.