Issue With DataGrid View

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aapy
    New Member
    • Apr 2010
    • 2

    Issue With DataGrid View

    I am trying to write a logic where upon selection of a row it should fill the form based on the value of the row selected from the datagrid view...

    But it is giving "select one row" message only on selecting few cells only
    and also i am not able to fill up the form even upon selection of a row

    kindly help
    this is my code
    Code:
    private void kryptonDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                
                int selectedCount = kryptonDataGridView1.SelectedRows.Count;
                if (selectedCount != 1)
                {
                    MessageBox.Show("Please select one row");
                    return;
                }
                else
                {
                    string block_id = Convert.ToString(kryptonDataGridView1.SelectedRows[0].Cells[0].Value);
                    MessageBox.Show(block_id);
                    try
                    {
                        connectioncls c = new connectioncls();
                        SqlConnection cn = c.connection();
                        cn.Open();
                        SqlCommand cmd = new SqlCommand("SELECT block_name FROM block WHERE block_id = '" + block_id + "'");
                        cmd.Connection = cn;
                        string ename = Convert.ToString(cmd.ExecuteScalar());
                        textBox7.Text = ename;
                        cn.Close();
                        cn.Open();
                        SqlCommand cmd1 = new SqlCommand("Select description from block where block_id = '" + block_id + "'");
                        cmd1.Connection = cn;
                        string mob_num = Convert.ToString(cmd1.ExecuteScalar());
                        textBox6.Text = mob_num;
                        cn.Close();
                        textBox8.Text = block_id;
    
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show("Connection error:" + exp.ToString());
                    }
    
    
                }
            }
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    This is what i would do. I would add a button control with the gridview control and name it select. then add a mygrid_RowComma nd() to process the data and apply it to the form.

    Code:
    protected void mygrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {      
    
            
            if (e.CommandName.Equals("id"))  // id is the identification for the button inside the gridview
            {
                // process the data here and fill the form as desired
            }

    Comment

    • aapy
      New Member
      • Apr 2010
      • 2

      #3
      how to add a button control to gridview control
      please give an idea for it too

      Comment

      Working...