CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2006
    Posts
    38

    How to read acontent of a cell in datagrid view

    Hi Every Body
    I'm a new in VC#.net and want to know How to read the content of a cell in datagridview control.
    for exampel i wrote the code below to add an items to the datagridview cells

    DataTable table = new DataTable();
    DataColumn nameCol;
    DataColumn emailCol;
    DataColumn telNumCol;
    DataRow row = table.NewRow();
    row[nameCol] = telRecordObj.GSName;
    row[emailCol] = telRecordObj.GSEmail;
    row[telNumCol] = telRecordObj.GSTeleNumber;
    table.Rows.Add(row);
    dataGridView1.DataSource = table.DefaultView;

    now what's the code for reading the content of a row of cells

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: How to read acontent of a cell in datagrid view

    I havn't tested yet as I dont know the names of your DataColumns but it should look something like
    Code:
    string name = (string)dataGridView1.Rows[row].Cells["do columnName  as string here"].Value;
    // like in your examplestring name =(string)dataGridView1.Rows[0].Cells["First Name"].Value;
    // or you also can do
    string name =(string)dataGridView1.Rows[0].Cells[0].Value;
    // or also this way
    string name =(string)dataGridView1.Rows[0].Cells[nameCol.ColumnName  ].Value;
    This should work. BTW I cannot see your DataColumn working as they are not initialized, the dataType isn't set and all that.
    Why instead of this, you are not using an existing database and filling a DataTable using a DataAdapter ?
    If you want to do it as you have typed it here it needs to be
    Code:
       DataTable table = new DataTable();
       // you need to initialize the Column objects and at least set a headertext
       // you also can format the columns in the DataColumns like setting datatypes ...   DataColumn nameCol = new DataColumn("First Name");
       DataColumn emailCol = new DataColumn("Email");
       DataColumn telNumCol = new DataColumn("Phone");
       // You need to add the DataColumns to the table   table.Columns.AddRange(new DataColumn[]{ nameCol,emailCol,telNumCol }); 
       DataRow row = table.NewRow();
       row[nameCol] = "Brown Joe"; // as I cannot test your objects I used imple strings for testing example   row[emailCol] = "[email protected]";
       row[telNumCol] = "0666 66 69 66";
       table.Rows.Add(row);
       dataGridView1.DataSource = table.DefaultView;
    BTW Please read forumrules and use Code Tags as you can see how to do in the signature lines of my post
    Last edited by JonnyPoet; March 22nd, 2009 at 05:19 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Mar 2006
    Posts
    38

    Re: How to read acontent of a cell in datagrid view

    Thanks JonnyPoet
    It works fine,with respect to my code i want tell that the code i wrote is a part of my project which is a bout telephone index which take my data(Name,Tel Number,Email address) from text box and store the data in telRecord object and store that object in a sequential file,the datagrid are filled from that file at the beginning of the application

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: How to read acontent of a cell in datagrid view

    Quote Originally Posted by Falcon Eyes View Post
    ...
    It works fine,with respect to my code i want tell that the code i wrote is a part of my project which is a bout telephone index ...
    I thought something like that and that this isn't your problem. But I didn't want to create this class as I think you have done that yourself. So I simple used strings in my example
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Dec 2006
    Posts
    38

    Re: How to read acontent of a cell in datagrid view

    BTW

    I find it easier to write (and for others to understand):
    Code:
        string abc = (string)dataGridView1[0,1].Value;

    than

    Code:
       string abc = (string)datagridview.row[1].cell[0].Value;

  6. #6
    Join Date
    Mar 2006
    Posts
    38

    Re: How to read acontent of a cell in datagrid view

    you all right k_nemelka it's more easier
    any way thanks to JonnyPoet & k_nemelka

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Wink Re: How to read acontent of a cell in datagrid view

    Quote Originally Posted by k_nemelka View Post
    BTW
    I find it easier to write (and for others to understand):
    Code:
     
        string abc = (string)dataGridView1[0,1].Value;
    than
    Code:
       string abc = (string)datagridview.row[1].cell[0].Value;
    You are right that I havn't shown all ways to solve his problem. But I really disagree about whats easier to understand. For a newbie you can easily see which index is for row and which for column if you write it in an full expressive way. You are right, if you are talking about what is shorter to type. Because having both indexes in one bracket, who will tell you reading that, which value the row and which is column index is. Then you only can see it in the itellisence help but not here in forum. So in which way you think its easier to understand ???
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Mar 2006
    Posts
    38

    Thumbs up Re: How to read acontent of a cell in datagrid view

    Don't be angry JonnyPoet booth of u r right

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: How to read acontent of a cell in datagrid view

    Quote Originally Posted by Falcon Eyes View Post
    Don't be angry JonnyPoet booth of u r right
    hehe - little diplomat , arn't you -
    But if no one should not use this bad 'Smilies', why they are there
    BTW Nothing is eaten as hot as it comes to the table.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured