Can't add row to datagridview?


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"?
 
Ad

Advertisements

S

Scott M.

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
 
A

Arun Babu

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);
}
 
Ad

Advertisements

A

Arun Babu

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);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top