Clearing row selected from DataGridView


E

Earl

I've got a datagridview with a bindingsource bound to a datatable. I've also
got a bunch of textboxes (on the same form) with their databindings set to
the datagridview (I'm not allowing edits in the datagridview). When I go to
add a new record, that is, by clearing the textboxes so the user may enter
what they wish, I need to ensure there is no binding between the textboxes
and the datagridview. So what I'm doing is setting the CurrentRow.Selected
property to false. This leaves the pointer and does not clear the textboxes.
I've never used much data-binding in the past, preferring to do this all by
hand, but I'd like to make this scenario work. My question is, do I *really*
have NO row now selected in the datasource and datagridview? I would think
that the bound textboxes would clear when I set the datagridview
CurrentRow.Selected to false.
 
Ad

Advertisements

J

Jim Hughes

Why not bind the datagridview AND the textboxes to a BindingSource?

Then set the datasource of the BindingSource to the datatable?

(Assuming ADO.Net 2.0)
 
E

Earl

Thanks for the idea Jim. That's what I love about development -- there's
always more than one way to do anything.
 
E

Earl

I looked at that again -- the binding source was already bound to the
datatable; the datatable was already bound to the datagrid as well as the
textboxes. So I'm left with the original question: have I totally deselected
a row within the bindingsource by setting the datagrid CurrentRow.Selected
property to false?
 
J

Jim Hughes

hence the problem grid and textboxes pointing to two places.

You should have the follwing heirarchy (don't you love ASCII graphics)

Datatable - > BindingSource
BindingSource -> Grid
BindingSource -> Textboxes

When you bind the grid to the datatable, you are getting a "default" binding
context that is different than the one that the BindingSource is using.
 
E

Earl

That was indeed incorrect, but didn't fix what I was trying to do. I found
that where I was instantiating the bindingsource was part of the issue. Here
is some of the code to show how I resolved it. This 1.) adds the new row to
the grid, 2.) positions the bindingsource on the last row, and 3.) clears
the textboxes. I'm still somewhat baffled that the textboxes do not clear
when I land on the last (empty) row.

private void frm_Load(object sender, EventArgs e)
{
bindingSource = new BindingSource();
}

private void PopulateGrid()
{
..... // populating the datatable

bindingSource.DataSource = dtForGrid;
dgv.DataSource = bindingSource;
}

private void btnNew_Click(object sender, EventArgs e)
{
bindingSource.AddNew();
bindingSource.MoveLast();
ClearTextPanel();
}
 
Ad

Advertisements

J

Jim Hughes

I'm not in place where I can test this, but IIRC, try this

private void btnNew_Click(object sender, EventArgs e)
{
bindingSource.Position = bindingSource.AddNew();
}

AddNew returns the position (index) of the row that was newly added. The
textboxes should get the default values (if speciified) or emptied.
 
Ad

Advertisements

E

Earl

Thanks Jim, that's a good thought. Is there a better example somewhere on
the bindingsource than I've found on the MSDN?
 

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