|
Is there a way to insert a row at the currently selected index into a data bound datagridview? I've been able to successfully add a row at a particular index if the datagridview is unbound. I can also add a row at the bottom of the list if the datagridview is bound. However, I need to add a row at a specified index into the list with the datagridview being databound. Does anyone know how to do this? | | MethodMas Friday, August 28, 2009 3:52 PM | You add the row to the underlying data source. So if you are bound to a BindingSource, you add to the BindingSource. If you are bound directly to a DataTable, you add the row to the DataTable. Hope this helps. www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | DeborahK Friday, August 28, 2009 5:10 PM | Try using .Insert instead of AddNew www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Proposed As Answer byDeborahKMVPSunday, August 30, 2009 4:33 AM
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | DeborahK Friday, August 28, 2009 5:22 PM | I figured it out. In this particular case, when I bind an "object" to DataGridView.DataSource through the designer, the DataSource property becomes a BindingSource object. So for example, say I create a DataGridView called "siteDataGridView", and using the designer I bind siteDataGridView to an "object" (class, really) called "Site". Then in order to programatically add a Site object to the underlying collection I can cast siteDataGridView.DataSource to BindingSource, and use BindingSource.Add():
public void addSite(Site s)
{
BindingSource bs = (BindingSource)this.siteDataGridView.DataSource;
bs.Add(s);
}
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | iiwoeirie Saturday, August 29, 2009 9:06 PM | Hi,
As @Deborahk said, if it’s databound, you need to insert the value in the datasource.
private void Form3_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'factoryDataSet.dt01' table. You can move, or remove it, as needed.
this.dt01TableAdapter.Fill(this.factoryDataSet.dt01);
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = factoryDataSet.dt01.NewRow();
dr[0] = 101;
factoryDataSet.dt01.Rows.InsertAt(dr, 0);
}
More information:
DataRow Class
http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | Ling Wang Friday, September 04, 2009 8:19 AM | You add the row to the underlying data source. So if you are bound to a BindingSource, you add to the BindingSource. If you are bound directly to a DataTable, you add the row to the DataTable. Hope this helps. www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | DeborahK Friday, August 28, 2009 5:10 PM | I have been able to successfully do this using bindingsourcename.addnew. However, this only adds to the end of the list. How do I add at a specific point in the list? | | MethodMas Friday, August 28, 2009 5:13 PM | Try using .Insert instead of AddNew www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Proposed As Answer byDeborahKMVPSunday, August 30, 2009 4:33 AM
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | DeborahK Friday, August 28, 2009 5:22 PM | Insert requires the parameters insert(index as integer, value as Object)
I just want to insert a new record. so, I'm not sure what to fill in for the value parameter. | | MethodMas Friday, August 28, 2009 5:39 PM | Could you just put New Customer (or whatever your class name is) in as the parameter? www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! | | DeborahK Friday, August 28, 2009 6:38 PM | I'm not sure if this is exactly what the OP was referring to, but I'm completely confused here. I have a DataGridView object. I assigned a DataSource using the designer. I was asked to create a data source. I selected "Object" and then browsed to a class called "Site" (It's a class, by the way, not an object so I don't quite understand why the dialog said "Object"). The DataGridView comes up and presents the properties of Site. I can add and delete rows. Now, I presume that somewhere hidden from me there is a collection of Sites which is bound to the DataGridView, and when I add a row through the gui, I presume a new Site object is added to this hidden collection. My confusion is, how do I get hold of that collection? Or if I can't get the collection, how do I add a new Site object to the collection? I could do DataGridView.rows.add(object[]) and pass the individual properties of the Site object, and I presume there would be a new Site object in the hidden collection, but that's not what I want to do. I have a Site object in my hand, and I want to add it to the automatically-created, hidden collection which is bound to the DataGridView. If I'm not being clear, it's because I'm totally confused. Any help appreciated. I'd be delighted to create the underlying collection myself, but I don't see any way to bind that collection to the DataGridView through the designer. In the Create Data source dialog there is an option for Object, but that doesn't allow me to bind objects, it allows me to bind classes, which I totally don't understand. For example, I can create a List<Site> or BindingList<Site>, but there doesn't seem to be any way in the designer to bind the DataGridView to that list object.
| | iiwoeirie Saturday, August 29, 2009 3:49 PM | I figured it out. In this particular case, when I bind an "object" to DataGridView.DataSource through the designer, the DataSource property becomes a BindingSource object. So for example, say I create a DataGridView called "siteDataGridView", and using the designer I bind siteDataGridView to an "object" (class, really) called "Site". Then in order to programatically add a Site object to the underlying collection I can cast siteDataGridView.DataSource to BindingSource, and use BindingSource.Add():
public void addSite(Site s)
{
BindingSource bs = (BindingSource)this.siteDataGridView.DataSource;
bs.Add(s);
}
- Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | iiwoeirie Saturday, August 29, 2009 9:06 PM | There is also a BindingList class that you can build yourself that is basically a collection of objects from a class. So if you wanted to build your own list, you could assign the BindingList to the datasource. Hope this helps. www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! | | DeborahK Sunday, August 30, 2009 4:44 AM | Hi,
As @Deborahk said, if it’s databound, you need to insert the value in the datasource.
private void Form3_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'factoryDataSet.dt01' table. You can move, or remove it, as needed.
this.dt01TableAdapter.Fill(this.factoryDataSet.dt01);
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = factoryDataSet.dt01.NewRow();
dr[0] = 101;
factoryDataSet.dt01.Rows.InsertAt(dr, 0);
}
More information:
DataRow Class
http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorSunday, September 06, 2009 1:40 PM
-
| | Ling Wang Friday, September 04, 2009 8:19 AM |
|