I was working on C# windows application as front end and SQL as back end I have display my sql table data in dtatgridview.

I want to display the data in textbox whatever row i select in datagridview.

I have write some code but its not showing data in textboxes.

here is my code:

the code is on datagridview cellcontentclick event

cn.ConnectionString = "Data Source=sam-AB59A9C19;Initial Catalog=master;Integrated Security=True";
            cn.Open();


            SqlCommand cmd = new SqlCommand();
            SqlDataReader rdr;
            string cd = dataGridView1.SelectedRows.ToString();
            string CommandText = "select * from acc where id=@id";

            cmd = new SqlCommand(CommandText);
            cmd.Connection = cn;
            cmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.VarChar, 20, "id"));
            cmd.Parameters["@id"].Value = dataGridView1.SelectedRows.ToString();
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                textBox1.Text = rdr["id"].ToString();
                textBox2.Text = rdr["sub"].ToString();   
                textBox3.Text = rdr["marks"].ToString();
            }
            cn.Close();

the code does not display any error message.

hi

If you want the values of the selected rows of the content in the textbox then you can directly write this code on Datagridview_Cell Click
Textbox1.Text=Results.Rows[e.RowIndex].Cells["your Cloumn Name"].Value.Tostring();

Results.Rows[e.RowIndex].Cells["your Cloumn Name"].Value.Tostring();

this line stends for
e.RowIndex= your selected rows and
Cells["your Cloumn Name"].Value= your column name which you have selecetd Row
So put the Code in gidview_RowEnter Event .

YOu cannot read a whole row of the dgv to a string. Ok, you can, but there will be no value you want to have. So you have to specify from which column and from which row you want to get the value.

Lets repair your code:

//just make sure you initialize SqlConnection!!
            cn.ConnectionString = "Data Source=sam-AB59A9C19;Initial Catalog=master;Integrated Security=True";                                  
            string cd = dataGridView1[0, e.RowIndex].Value.ToString(); //0 represens the 1st column 
            //!!! change the number to the column you want to get the correct value !!!

            string CommandText = "select * from acc where id = @id";
            SqlCommand cmd = new SqlCommand(CommandText, cn);
            cmd.Connection = cn;
            cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar, 20).Value = cd;
            cn.Open();  
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                textBox1.Text = (string)rdr[0];
                textBox2.Text = (string)rdr[1];   
                textBox3.Text = (string)rdr[2];
            }
            cn.Close();

This should work.

Use This code this will pick the contents of the current cell and place in the textBox

{
//rest of the code
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellClick);
//rest of the code
}

 void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
                textBox1.Text=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }

and if you want the whole record then you can use more than one textbox to display them like

void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
                textBox1.Text=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
//.....
texBoxn.Text=dataGridView1.Rows[e.RowIndex].Cells[n+1].Value.ToString();

            }

Edited 5 Years Ago by abelLazm: n/a

Hi if i want to take strings var from each columns in my datagridview and use them in another form in the same project how can i achieve that?

for example

Method1(Something1, Something2);

void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
string Something1=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string Something2=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
           }

Method1 is in the same WFA or in another

I m new on programming!

Edited 1 Year Ago by Ιωάννης: wrong Cells

This article has been dead for over six months. Start a new discussion instead.