Can't add row to datagridview?

  • Thread starter Thread starter Grant Schenck
  • Start date Start date
G

Grant Schenck

Hello,

I'm trying to programmatically add a row to a SQL bound (via binding source)
to a datagridview.

The code is as follows:

DataRowView drView =
(DataRowView)stationBindingSource.AddNew();
STPSRecorderDataSet.StationRow stationRow =
(STPSRecorderDataSet.StationRow)drView.Row;
stationRow.Extension = strExtensionToAdd;

It works fine and if I add more then one row they show up.

Now the problem is that if I move to a different record and then move back
the last row I added is removed. Also, if before selecting a different
record I manually select another row in the view, then, when I come back the
record does not disappear.

Does the code above look correct for adding a row to a bound DGV? Why does
the last row not "stick"?
 
Grant Schenck said:
Hello,

I'm trying to programmatically add a row to a SQL bound (via binding
source) to a datagridview.

The code is as follows:

DataRowView drView =
(DataRowView)stationBindingSource.AddNew();
STPSRecorderDataSet.StationRow stationRow =
(STPSRecorderDataSet.StationRow)drView.Row;
stationRow.Extension = strExtensionToAdd;

It works fine and if I add more then one row they show up.

Now the problem is that if I move to a different record and then move back
the last row I added is removed. Also, if before selecting a different
record I manually select another row in the view, then, when I come back
the record does not disappear.

Does the code above look correct for adding a row to a bound DGV? Why
does the last row not "stick"?

It seems to me that you are adding the row correctly, but then the DGV is
just rebinding to its original data source. What I would do is add the data
that was placed into the DGV into the underlying data source and when the
DGV attempts to rebind, you'll have the new data in the grid.

-Scott
 
ok,the following code snipe ct may help u. sorry for the less information.

SqlDataReader rdr = comm1.ExecuteReader();
DataView conta = (DataView)lstDue1.Items.SourceCollection;
DataRowView contact = conta.AddNew();
while (rdr.Read())
{
contact[0] = rdr["ID"];
contact[1] = rdr["dFactor"];
lstDue2.Items.Add(contact);
}
 
ok,the following code snipe ct may help u. sorry for the less information,

SqlDataReader rdr = comm1.ExecuteReader();
DataView conta = (DataView)lstDue1.Items.SourceCollection;
DataRowView contact = conta.AddNew();
while (rdr.Read())
{
contact[0] = rdr["ID"];
contact[1] = rdr["dFactor"];
lstDue2.Items.Add(contact);
}

Hello,

I am trying to programmatically add a row to a SQL bound (via binding source)
to a datagridview.

The code is as follows:

DataRowView drView =
(DataRowView)stationBindingSource.AddNew();
STPSRecorderDataSet.StationRow stationRow =
(STPSRecorderDataSet.StationRow)drView.Row;
stationRow.Extension = strExtensionToAdd;

It works fine and if I add more then one row they show up.

Now the problem is that if I move to a different record and then move back
the last row I added is removed. Also, if before selecting a different
record I manually select another row in the view, then, when I come back the
record does not disappear.

Does the code above look correct for adding a row to a bound DGV? Why does
the last row not "stick"?

--
Grant Schenck
On Thursday, October 15, 2009 11:54 AM Scott M. wrote:
It seems to me that you are adding the row correctly, but then the DGV is
just rebinding to its original data source. What I would do is add the data
that was placed into the DGV into the underlying data source and when the
DGV attempts to rebind, you will have the new data in the grid.

-Scott
On Tuesday, January 18, 2011 4:13 AM Arun Babu wrote:
ok,the following code snipe ct may help u. sorry for the less information.



SqlDataReader rdr = comm1.ExecuteReader();

DataView conta = (DataView)lstDue1.Items.SourceCollection;

DataRowView contact = conta.AddNew();

while (rdr.Read())

{

contact[0] = rdr["ID"];

contact[1] = rdr["dFactor"];

lstDue2.Items.Add(contact);

}
 
Back
Top