473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Adding data by column name to DataGridView in C#

I've added a DataGridView and added some columns in the designer, now I want to add some rows with data applied via column name.

Having done some web trawling I've ended up with this:
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.             DataGridViewRow row = new DataGridViewRow();
  4.             row.CreateCells(MyGrid);     // #1
  5.             row.Cells[0].Value = "Add";  // #2 this makes it work
  6.             MyGrid.Rows.Add(row);       // #3 need this before adding data
  7.             row.Cells["Selected"].Value = "Add";
  8.             row.Cells["Model"].Value = thing;
  9.             row.Cells["Description"].Value = thingdesc;
  10.             row.Cells["Scenario"].Value = "Basic";
  11.             row.Cells["ModelLoop"].Value = "1";
  12.             row.Cells["LaunchCount"].Value = "1";
  13. }
  14.  
Apparently I need lines marked # above for it to work. Without #2 the lines after #3 come back with System.ArgumentOutOfRangeException. It seems that without #2, CreateCells() doesn't.

Also the data has to be added after #3. Putting it between #2 & #3 gives e.g. Column named Selected cannot be found.

I would have thought #1 was sufficient to allow me to add the data.

Any thoughts?

Simon
Nov 2 '07 #1
3 35498
Plater
7,872 Expert 4TB
There is a slightly quicker way, "MyGrid" is your datagridview?
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.    int idx = MyGrid.Rows.Add();
  4.    DataGridViewRow row = MyGrid.Rows[idx];
  5.    row.Cells["Selected"].Value = "Add";
  6.    row.Cells["Model"].Value = thing;
  7.    row.Cells["Description"].Value = thingdesc;
  8.    row.Cells["Scenario"].Value = "Basic";
  9.    row.Cells["ModelLoop"].Value = "1";
  10.    row.Cells["LaunchCount"].Value = "1";
  11. }
  12.  
Nov 2 '07 #2
Excellent! Works a treat.

Many thanks.
Nov 6 '07 #3
I've added a DataGridView and added some columns in the designer, now I want to add some rows with data applied via column name.

Having done some web trawling I've ended up with this:
Expand|Select|Wrap|Line Numbers
  1. private void AddRow(string thing,string thingdesc)
  2. {
  3.             DataGridViewRow row = new DataGridViewRow();
  4.             row.CreateCells(MyGrid);     // #1
  5.             row.Cells[0].Value = "Add";  // #2 this makes it work
  6.             MyGrid.Rows.Add(row);       // #3 need this before adding data
  7.             row.Cells["Selected"].Value = "Add";
  8.             row.Cells["Model"].Value = thing;
  9.             row.Cells["Description"].Value = thingdesc;
  10.             row.Cells["Scenario"].Value = "Basic";
  11.             row.Cells["ModelLoop"].Value = "1";
  12.             row.Cells["LaunchCount"].Value = "1";
  13. }
  14.  
Apparently I need lines marked # above for it to work. Without #2 the lines after #3 come back with System.ArgumentOutOfRangeException. It seems that without #2, CreateCells() doesn't.

Also the data has to be added after #3. Putting it between #2 & #3 gives e.g. Column named Selected cannot be found.

I would have thought #1 was sufficient to allow me to add the data.

Any thoughts?

Simon

HEllo
DO One Thing
private void AddRow(string thing,string thingdesc)
{
string [] row1 = { comumn1(Value), comumn2(Value),........}
string [] row2= { comumn1(Value), comumn2(Value),.........}

datatgridView1.add(row1);
datatgridView1.add(row2);

}


Thats all
Hope This will help You

Regards
Ramesh
Nov 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Shino | last post by:
Hi, Can anyone help with this error: "ORA-00904: invalid column name"? Thanks! SQL> create view PPFa as 2 SELECT L.UserID AS LecID, U.Name, U.Email, I.IntakeID, S.UserID AS StudID 3 FROM...
2
by: Chris Cobb | last post by:
I have a table that currently contains 718000 rows. I wish to add a column to the table. Adding this column anywhere other than the end of the table requires exporting data, a drop and recreate,...
3
by: Raj | last post by:
Hi, I am trying to add some more information to the table which already has a lot a data (like 2-3000 records). The new information may be adding 2-3 new columns worth. Now my questions are:...
6
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
2
by: tony lock | last post by:
Has anybody found a way to disable a column in DataGridView i.e prevent it being selected. With DataGrid it was fairly easy to check on the column being entered and move the selection to the next...
7
by: BillE | last post by:
What is the best way to add data using a DataGridView in a multi-tier application (data entry is handled in a data access layer using stored procedures)? Thanks Bill
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.