CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: DataGridView

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    DataGridView

    I have a datagridview on a form. the datagridview has multiple rows and columns. When i select a row, for example 3rd row, i want to get the value of specified column, for example 4th. I mean when i choose 3rd row, i want to get the value of the cell "3rd Row X 4th Column". How can i manage this.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: DataGridView

    There are a number of events you could use. I normally use the CellClick event.

    Code:
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
         string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    }
    That will give you the value of the cell that was clicked.

    If you to get the 4th column of the 3rd row, you could do this...

    Code:
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
         string value = dataGridView1.Rows[2].Cells[3].Value.ToString();
    }
    And last, if you want to get the data from the 4th column of the row that was selected..

    Code:
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
         string value = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    }
    Remember that the columns and rows are both 0-indexed. So a row with an index of 0 is actually the first row.
    ===============================
    My Blog

  3. #3
    Join Date
    Apr 2010
    Posts
    5

    Re: DataGridView

    thank you very much for your detailed explanation..

Tags for this Thread

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