Home All Groups Group Topic Archive Search About

Clearing row selected from DataGridView

Author
9 Oct 2006 6:59 AM
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.

Author
9 Oct 2006 3:20 PM
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)

Show quote
"Earl" <brikshoe@newsgroups.nospam> wrote in message
news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
> 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.
Author
9 Oct 2006 4:52 PM
Earl
Thanks for the idea Jim. That's what I love about development -- there's
always more than one way to do anything.

Show quote
"Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
> 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)
>
> "Earl" <brikshoe@newsgroups.nospam> wrote in message
> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>> 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.
>
>
Author
9 Oct 2006 4:57 PM
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?

Show quote
"Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
> 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)
>
> "Earl" <brikshoe@newsgroups.nospam> wrote in message
> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>> 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.
>
>
Author
9 Oct 2006 9:19 PM
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.

Show quote
"Earl" <brikshoe@newsgroups.nospam> wrote in message
news:uPjYzP86GHA.4304@TK2MSFTNGP03.phx.gbl...
>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?
>
> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
> news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
>> 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)
>>
>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>>> 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.
>>
>>
>
>
Author
10 Oct 2006 2:20 AM
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();
}


Show quote
"Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
news:uzo6mi%236GHA.756@TK2MSFTNGP05.phx.gbl...
> 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.
>
> "Earl" <brikshoe@newsgroups.nospam> wrote in message
> news:uPjYzP86GHA.4304@TK2MSFTNGP03.phx.gbl...
>>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?
>>
>> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
>> news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
>>> 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)
>>>
>>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>>> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>>>> 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.
>>>
>>>
>>
>>
>
>
Author
10 Oct 2006 1:20 PM
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.

Show quote
"Earl" <brikshoe@newsgroups.nospam> wrote in message
news:eoqXiKB7GHA.1248@TK2MSFTNGP03.phx.gbl...
> 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();
> }
>
>
> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
> news:uzo6mi%236GHA.756@TK2MSFTNGP05.phx.gbl...
>> 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.
>>
>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>> news:uPjYzP86GHA.4304@TK2MSFTNGP03.phx.gbl...
>>>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?
>>>
>>> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
>>> news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
>>>> 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)
>>>>
>>>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>>>> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>>>>> 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.
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
10 Oct 2006 8:39 PM
Earl
Thanks Jim, that's a good thought. Is there a better example somewhere on
the bindingsource than I've found on the MSDN?

Show quote
"Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
news:u7Vsc7G7GHA.3760@TK2MSFTNGP02.phx.gbl...
> 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.
>
> "Earl" <brikshoe@newsgroups.nospam> wrote in message
> news:eoqXiKB7GHA.1248@TK2MSFTNGP03.phx.gbl...
>> 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();
>> }
>>
>>
>> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
>> news:uzo6mi%236GHA.756@TK2MSFTNGP05.phx.gbl...
>>> 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.
>>>
>>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>>> news:uPjYzP86GHA.4304@TK2MSFTNGP03.phx.gbl...
>>>>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?
>>>>
>>>> "Jim Hughes" <NOSPAMJ3033@Hotmail.com> wrote in message
>>>> news:eKD17Z76GHA.756@TK2MSFTNGP05.phx.gbl...
>>>>> 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)
>>>>>
>>>>> "Earl" <brikshoe@newsgroups.nospam> wrote in message
>>>>> news:eqROEC36GHA.3644@TK2MSFTNGP03.phx.gbl...
>>>>>> 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.
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

AddThis Social Bookmark Button