473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding data by column name to DataGridView in C#

12 New Member
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.Argument OutOfRangeExcep tion. 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 35628
Plater
7,872 Recognized Expert Expert
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
SimonHeffer
12 New Member
Excellent! Works a treat.

Many thanks.
Nov 6 '07 #3
rameshb106
1 New Member
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.Argument OutOfRangeExcep tion. 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
64803
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 User_Lecturer L, User_Student S, TBL_Intake I, Users U 4 WHERE L.UserID=S.InitialSupervisorID And L.UserID=U.UserID And
2
1963
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, then a re-import of data. Is there any disadvantage to adding this column to the end of the table. Will this fragment the table. Is a reorg needed? ...
3
4896
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: (1)Is it a good idea to add new columns to the existing table? then it will create these new columns for all old records, will it not result in wasting...
6
3225
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 entry application. The code below gets line items from a SQL Server database and returns them to a datagrid by way of a DataTable called...
3
1941
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 example would be: Let's say Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and Column2, I need to do some calculations and...
6
6398
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 gave it a try using the example given in
2
13371
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 column, this does not seem possible with DataGridView.
7
7331
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
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7480
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5373
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.