locked
Select a row, by clicking in a cell in Data Grid View RRS feed

  • Question

  • I have a Data Grid View that can perform edits to a SQL database, however currently I have to select a whole row, instead of just a single cell, how do I make it so that the user can select one cell and from there i can get the row.

    In simple terms, I currently use a "this.dataGridView1.SelectedRows" how do I change this so that it still performs the same but I can click in any cell within the row.

            private void btnEdit_Click(object sender, EventArgs e)
            {
                Update update = new Update();
                update.TokenID = this.dataGridView1.SelectedRows[0].Cells["TokenID"].Value.ToString();
                update.ShowDialog();
                LoadGrid();
            }

    Thanks in advance for your help

    Ryan

    Monday, July 30, 2012 3:22 PM

Answers

  • Hi, 

    I would use CellClick event and retreive data from DataGridViewRow class:

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {           
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (!row.IsNewRow)
                {
                    string c1 = row.Cells["c1"].ToString();
                    string c2 = row.Cells["c2"].ToString();
                }
            }

    regarding that the c1 and c2 are names of columns respectively.


    Mitja

    • Edited by Mitja Bonca Monday, July 30, 2012 3:49 PM
    • Proposed as answer by Norkk Monday, July 30, 2012 4:00 PM
    • Marked as answer by Lisa Zhu Wednesday, August 1, 2012 1:59 AM
    Monday, July 30, 2012 3:48 PM
  • change SelectionMode property on the datagridview to CellSelect instead of FullRowSelect

    "footballinblue" wrote in message news:4a1ecdb0-de97-4ea7-8807-4afd094e4faa@communitybridge.codeplex.com...

    I have a Data Grid View that can perform edits to a SQL database, however currently I have to select a whole row, instead of just a single cell, how do I make it so that the user can select one cell and from there i can get the row.

    In simple terms, I currently use a "this.dataGridView1.SelectedRows" how do I change this so that it still performs the same but I can click in any cell within the row.

                 private void btnEdit_Click(object sender, EventArgs e)
                 {
                         Update update = new Update();
                         update.TokenID =
    this.dataGridView1.SelectedRows[0].Cells["TokenID"].Value.ToString();
                         update.ShowDialog();
                         LoadGrid();
                 }

     Thanks in advance for your help

    Ryan

    • Marked as answer by Lisa Zhu Wednesday, August 1, 2012 1:59 AM
    Monday, July 30, 2012 4:04 PM
  • Found the issue, the simplest way of doing it, is when the form loads

    e.g. in

            private void Form1_Load(object sender, EventArgs e)
            {
                LoadGrid();
            }

    or in my case through my LoadGrid() method you need to change the selection method using the code...

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    


    Ryan

    Tuesday, July 31, 2012 2:11 PM

All replies

  • Hi, 

    I would use CellClick event and retreive data from DataGridViewRow class:

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {           
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (!row.IsNewRow)
                {
                    string c1 = row.Cells["c1"].ToString();
                    string c2 = row.Cells["c2"].ToString();
                }
            }

    regarding that the c1 and c2 are names of columns respectively.


    Mitja

    • Edited by Mitja Bonca Monday, July 30, 2012 3:49 PM
    • Proposed as answer by Norkk Monday, July 30, 2012 4:00 PM
    • Marked as answer by Lisa Zhu Wednesday, August 1, 2012 1:59 AM
    Monday, July 30, 2012 3:48 PM
  • change SelectionMode property on the datagridview to CellSelect instead of FullRowSelect

    "footballinblue" wrote in message news:4a1ecdb0-de97-4ea7-8807-4afd094e4faa@communitybridge.codeplex.com...

    I have a Data Grid View that can perform edits to a SQL database, however currently I have to select a whole row, instead of just a single cell, how do I make it so that the user can select one cell and from there i can get the row.

    In simple terms, I currently use a "this.dataGridView1.SelectedRows" how do I change this so that it still performs the same but I can click in any cell within the row.

                 private void btnEdit_Click(object sender, EventArgs e)
                 {
                         Update update = new Update();
                         update.TokenID =
    this.dataGridView1.SelectedRows[0].Cells["TokenID"].Value.ToString();
                         update.ShowDialog();
                         LoadGrid();
                 }

     Thanks in advance for your help

    Ryan

    • Marked as answer by Lisa Zhu Wednesday, August 1, 2012 1:59 AM
    Monday, July 30, 2012 4:04 PM
  • Sorry to be really thick but how would I link this back into my edit button, thanks for your help

    ryan


    Ryan

    Tuesday, July 31, 2012 7:54 AM
  • CurrentRow property will do fine from your button event as well. So use exactly the same code:

    private void buttonEdit_Click(object sender, EventArgs e)
    {
         DataGridViewRow row = dataGridView1.CurrentRow;
         if (!row.IsNewRow)
         {
             string c1 = row.Cells["c1"].ToString();
             string c2 = row.Cells["c2"].ToString();
         }
    }


    Mitja

    Tuesday, July 31, 2012 12:01 PM
  • Found the issue, the simplest way of doing it, is when the form loads

    e.g. in

            private void Form1_Load(object sender, EventArgs e)
            {
                LoadGrid();
            }

    or in my case through my LoadGrid() method you need to change the selection method using the code...

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    


    Ryan

    Tuesday, July 31, 2012 2:11 PM