Solved

Adding a new row in an unbound DataGridView

Posted on 2011-09-27
2
357 Views
Last Modified: 2012-08-14
I have a datagridview and i was hoping there was an easy method to create a new row based off of the datagridview.

for example:
DataGridViewRow dataGridViewRow = dataGridView.Rows[dataGridView.Rows.Add()];

Open in new window


Obviously gives me an instance of the row with the proper columns already bound to the row however if it's the first row in the DataGridView this will fire the SelectionChanged event.


I'm looking to get the exact same row with the proper columns already attached to it but not have it be added to the collection first so I can assign the proper values AND THEN attach it to the datagridview via the Rows.Add function.


I though this was posssible...



Thanks
0
Comment
Question by:collages
2 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 36711822
maybe something like this:

    DataRowView rowView = myView.AddNew();
    // Change values in the DataRow.
    rowView["ColumnName"] = "New value";
    rowView.EndEdit();

then bind it to your dataGrid...
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 500 total points
ID: 36714505
Hi,

You can add rows to the grid as follows.
I have added three columns, 2 columns are textbox and third is check box,
accordingly i place the values for my row added to the grid.

 int index = dataGridView1.Rows.Add(); // Provides a row and adds to the collection
dataGridView1.Rows[index].Cells[0].Value = "1"; // Provide values for each row cells
dataGridView1.Rows[index].Cells[1].Value = "2";
dataGridView1.Rows[index].Cells[2].Value = "True";
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now