1

I have a table layout on my Form in which I use a Datagrid to show the data. The first time when I assign a datasource to it, it works well, but when I assign a Datasource to the DataGrid a second time, it doesn't call the RowsAdded event of Datagrid and Datagrid doesn't show anything except its header and Datagrid shows the RowCount is 0.

I found a similar problem here also:

Datagridview rowcount showing 0 even when there is a valid datasource

EDIT #1

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
Community
  • 1
  • 1
Mogli
  • 1,972
  • 11
  • 34
  • 67
  • 1
    please update the question with the code – Damith Nov 02 '13 at 15:42
  • i am assigning the data source using above code every time. – Mogli Nov 02 '13 at 16:13
  • in second time have you check you have records in `DataTable` or not? – Damith Nov 02 '13 at 16:27
  • I have checked that it contains rows, please see http://stackoverflow.com/questions/11212086/datagridview-rowcount-showing-0-even-when-there-is-a-valid-datasource – Mogli Nov 02 '13 at 17:01
  • I highly recommend you to add another grid to your form and fill this grid instead of your existing grid. Your problem is very strange, that's why no one can help you. You should also try using some **foreach** loop to loop through all the rows of your **dt**, try printing them out manually to see what rows are in there. – King King Nov 02 '13 at 17:09
  • i tried with the another grid also and it is behaving the same like first one. – Mogli Nov 03 '13 at 05:33

1 Answers1

1

Rows added event is fired when rows are added using the dataGridView.Rows.Add() method. You should use DataSourceChanged event to fire the change of the datasource of the dataGridView. Also checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasourcechanged.aspx and http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded.aspx

Hope this helps!

lauCosma
  • 154
  • 10