0

I am assigning a datasource to the datagridview and it works fine but only for the first time. When I assign the datasource a second time it doesn't show the data. My code is:

gridProjectEdit.DataSource = null;
gridProjectEdit.Columns.Clear();
gridProjectEdit.Rows.Clear();            
gridProjectEdit.Refresh();

if(dt!=null)
dt.Clear();
dt=methodCaller.GetProjectData(); //get the data
gridProjectEdit.DataSource = dt;  //copying datatable
copyOfProjectDataTable = dt.Copy(); //this datatable used to check sno

I also rename the column headers ahead like this, if it matters:

//renaming column header
gridProjectEdit.Columns[0].HeaderText = "S.NO.";
gridProjectEdit.Columns[1].HeaderText = "PROJECTNAME";

When I debugged this code the second time it showed the datatable having 6 rows but I don't know why it doesn't show the data, it shows only column headers.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Mogli
  • 1,972
  • 11
  • 34
  • 67
  • When you say the second time, what do you mean? Is the same code executed the second time? – Szymon Nov 02 '13 at 09:35
  • yes the same code is executed the second time. – Mogli Nov 02 '13 at 09:39
  • @Mogli it's obviously that the `GetProjectData` works improperly, it returns `empty data` the second time – King King Nov 02 '13 at 09:40
  • no GetProjectData() returns the datatable which contains 6 rows, i checked it. – Mogli Nov 02 '13 at 09:41
  • @Mogli that's the first time, did you check for the second time? – King King Nov 02 '13 at 09:42
  • yes i did, i wrote that in my question also. – Mogli Nov 02 '13 at 09:44
  • @Mogli at the time you checked it, it might have 6 rows but after that (for some reason) it might be empty. You should check all references of your `dt` variable, continue debugging until you find the problem. – King King Nov 02 '13 at 10:15
  • @KingKing i am still debugging and found something that RowCount of gridview is 0 even after assigning the datasource to it. Now this is the problem why i am not getting any data in gridview. But i don't know why it's occuring? ANY IDEA ?? – Mogli Nov 02 '13 at 13:05
  • when i assign the datasource to datagrid it calls the rowsadded event but second time it doesn't call the rowsadded event. any solution ? – Mogli Nov 02 '13 at 14:17
  • see this link my problem is something like this: http://stackoverflow.com/questions/11212086/datagridview-rowcount-showing-0-even-when-there-is-a-valid-datasource – Mogli Nov 02 '13 at 14:54

1 Answers1

0

It seems to me like you shouldn't assign your datasource several times. Set the DataSource once in the Constructor or the OnInitialize Method and instead of doing

dt=methodCaller.GetProjectData();

do

dt.AddRange(methodCaller.GetProjectData());
Wand
  • 1