PC Review


Reply
Thread Tools Rate Thread

Can't add row to datagridview?

 
 
Grant Schenck
Guest
Posts: n/a
 
      14th Oct 2009
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



 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      15th Oct 2009

"Grant Schenck" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


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


 
Reply With Quote
 
Arun Babu
Guest
Posts: n/a
 
      18th Jan 2011
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);
}



> On Tuesday, October 13, 2009 8:44 PM Grant Schenck wrote:


> 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



>> Submitted via EggHeadCafe
>> ASP.NET Base64 Image Encoding via the Data: protocol
>> http://www.eggheadcafe.com/tutorials...-protocol.aspx

 
Reply With Quote
 
Arun Babu
Guest
Posts: n/a
 
      18th Jan 2011
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);
}


> On Tuesday, October 13, 2009 8:44 PM Grant Schenck wrote:


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



>>> Submitted via EggHeadCafe
>>> SQL Server Best Practices
>>> http://www.eggheadcafe.com/tutorials...practices.aspx

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Refreshing Linq query to update second DataGridView when position in first DataGridView changes Arved Sandstrom Microsoft C# .NET 1 25th May 2008 04:06 AM
dataGridView.Rows.Count and datagridview.RowCount SePp Microsoft C# .NET 1 15th Jan 2008 10:27 AM
DataGridView (.NET 2.0 Windows Forms) Text wrapping in DataGridView Items Chris Hayes Microsoft Dot NET Framework 1 27th Aug 2006 06:07 PM
Clearing selection in DataGridView when user clicks outside the DataGridView control djyakov@gmail.com Microsoft Dot NET Framework Forms 0 16th Feb 2006 06:38 PM
Refreshing datagridview contents after a failed delete in bound datagridview Bob Microsoft VB .NET 0 5th Jan 2006 06:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:33 PM.