Click here to Skip to main content
14,331,456 members
Rate this:
Please Sign up or sign in to vote.
See more:
So, I am trying to use an Event from C# into the TextBox which could be able to after selecting something from a DataGridView it will insert that columns values in the TextBox and then detailed the info from what it is in the TextBox.

I don't want to use TextChanged

What could I do to after the value shows on TextBox it will go to DataGridView?

What I have tried:

public bool ValidateText()
            bool Isvalidated = false;

                SqlConnection con = new SqlConnection(cs.DBConnP);

                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand(@"", con);
                ds = new DataSet("ds");
                dtable = ds.Tables[0];

                adp.Fill(ds, "CargaCab");

                dataGridView1.DataSource = ds.Tables["CargaCab"].DefaultView;

                foreach (DataRow row in dt.Rows)
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = row[0].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = row[1].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = row[2].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = row[3].ToString();

                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = false;

            catch (Exception ex)
                MessageBox.Show("Erro\nDetalhes: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Isvalidated = false;
            return Isvalidated;

private void txtCarga_Validating(object sender, CancelEventArgs e)

            int LinhasAfetadas = 0;
            LinhasAfetadas = dataGridView1.RowCount;
            label1.Text = "Número de registos: " + LinhasAfetadas;
Updated 2-Feb-17 7:24am
CHill60 2-Feb-17 7:06am
Your question isn't entirely clear. However, if I wanted to update another control with information from a textbox I would do the update in the textbox.Validated event
Ramza360 2-Feb-17 13:12pm
Are you trying to populate the text box when clicking on a cell in the data grid view? As CHill60 stated, it's not clear.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming you mean populating the text box with what is selected in data grid view, use the CellClick or CellEnter events on the data grid view. CellEnter is more useful as it will trigger on cell click and when a cell is focused (i.e. using arrow keys to navigate in the grid).

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e){
   object data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;   
   txtCarga.Text = data.ToString();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100