Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / September 2006

Tip: Looking for answers? Try searching our database.

DataGridView Adding Rows

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chuck P - 13 Sep 2006 23:00 GMT
I would like to add a row to a bound datagridview by clicking on a
button and have the cursor placed on the new row and the
DataGridView's cells filled with default values. Also if the users
presses the ESC key for the new row the row would be removed (behavior
like in the * row).

My current code has:  BindingSource.AddNew();

When this gets executed the grid puts the focus on the first cell in
the added row.    The dataGridView1_DefaultValuesNeeded event does not
fire and the default values for the row are not set.

What is the best way to add a new row to the datagridview which has
defalut values and not have default values or events strung out all
over the code.

It would be nice if in the button code if there was just a way to get
the cursor to go to the first editable cell in the * row of the
datagridView.  Of course I would like to make my button code to work
generically whether or not there is a datagridView on the form or not.
Linda Liu [MSFT] - 14 Sep 2006 07:10 GMT
Hi Chuck,

As you have mentioned, there're two options to add a new row to a
datagridview by clicking on a button. One is to call the AddNew method of
bindingsource and the other is to set the current row to the * row in the
datagridview. Generally speaking, the first option is a common usage.
However, if you want to remove the new row if the user press the ESC key
without any code, the second option is a better candidate.

To set the currect row to the * row, we set the CurrentCell to the new row
actually. The following is a sample.

private void button1_Click(object sender, EventArgs e)
{
// make use of the NewRowIndex property of the datagridview to get the row
index of the * row
this.dataGridView1.CurrentCell =
this.dataGridView1.Rows[this.dataGridView1.NewRowIndex].Cells[0];
// Make the datagridview get focused, otherwise the datagridview won't
react when we press the ESC or any other key.
this.dataGridView1.Focus();
}

To set the default value of the new row, you also have two ways. One way is
to set the default value in the data source. For example, if you're using
datatable, set the DefaultValue property of each column in the datatable;
if the data source is a collection of object, set the default value in the
construct function of the class.

The other way depends on which option you adopt to add a new row in the
datagridview.  If you call the BindingSource.AddNew method to add a new
row, handle the AddingNew event of the bindingsource. If you set the
current row to the * row, handle the DefaultValuesNeeded event of the
datagridview.

I recommend you to adopt the first method to set the default value of the
new row. This method is more convenient.

As for the function to remove the new row if the user presses the ESC key,
as I have mentioned above, we needn't add a line of code for this function
when we set the current row to the * row. However, if we call the
BindingSource.AddNew method to add a new row, we should handle the KeyUp
event of the datagridview to do this. The following is a sample.

void dataGridView1_KeyUp(object sender, KeyEventArgs e)
     {
           if (e.KeyCode == Keys.Escape)
           {
               this.bindingSource1.RemoveCurrent();            
           }
      }

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Chuck P - 14 Sep 2006 16:08 GMT
Thanks,

I don't mind putting is some code as long as I can make it fairly
generic.

The bad things about using bindingSource.AddNew
  you don't get DefaultValuesNeeded event in the datagridview
  you can't escape out of the datagridview if you want to cancel the
row

The bad things about going to the * row
 My control with the add button needs to know whether the
bindingSource is bound to a dataGridView or not.  I can't just drop
the control on a form.

What would I have to do to get bindingSource.AddNew to
 use the * row in the dataGridview or at least be able to ESC out and
get the default values specified in the dataGridView.

 

>Hi Chuck,
>
[quoted text clipped - 74 lines]
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 15 Sep 2006 02:57 GMT
Hi Chuck,

> The bad things about going to the * row  
> My control with the add button needs to know whether the bindingSource is
bound to a dataGridView or not.  I can't just drop the control on a form.

Do you mean you don't have a datagridview on the form? Or you have a
datagridview on the form, but you don't bind a bindingsource to the
datagridview? If yes, could you tell me why?

> What would I have to do to get bindingSource.AddNew to use the * row in
the dataGridview or at least be able to ESC out and get the default values
specified in the dataGridView.

If you call BindingSource.AddNew to add a new row, a new row will be added
into the datagridview, which isn't the * row. If you want to delete the new
row when the ESC key is pressed, you should handle the KeyUp event of the
datagridview. If you want to set the default values for the new row, you
could set the default values in the datatable or the class's constructor.

If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Chuck@newsgroup.nospam - 15 Sep 2006 15:40 GMT
>Hi Chuck,
>
[quoted text clipped - 5 lines]
>datagridview on the form, but you don't bind a bindingsource to the
>datagridview? If yes, could you tell me why?

I was creating a control with navigation and add/save buttons.
Sometimes it's bindingsource would be attached to a grid, sometimes
not.

>> What would I have to do to get bindingSource.AddNew to use the * row in
>the dataGridview or at least be able to ESC out and get the default values
[quoted text clipped - 11 lines]
>Linda Liu
>Microsoft Online Community Support
Linda Liu [MSFT] - 18 Sep 2006 11:13 GMT
Hi Chuck,

I am sorry that I couldn't understand your scenario exactly.

Based on my understanding, you have a datagridview and a button on the form
and you bind the datagridview to a bindingsource. When you click the
button, you want to add a new row in the datagridview.

To add new row in the datagridview, you could call BindingSource.AddNew()
method or force the * row to be the current row in the datagridview for the
user to type a new row.

> I was creating a control with navigation and add/save buttons. Sometimes
it's bindingsource would be attached to a grid, sometimes not.

If the bindingsource isn't bound to the datagridview, what data source else
is bound to the datagridview?

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support
Chuck P - 18 Sep 2006 14:35 GMT
It would just be a form with no datagridviews on it.

>Hi Chuck,
>
[quoted text clipped - 19 lines]
>Linda Liu
>Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.