Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;

            try
            {
                SqlConnection con = new SqlConnection(cs.DBConnP);
                con.Open();

                adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand(@"", con);
                
                ds = new DataSet("ds");
                adp.Fill(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;

                con.Close();
            }
            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)
        {
            ValidateText();

            int LinhasAfetadas = 0;
            LinhasAfetadas = dataGridView1.RowCount;
            label1.Text = "Número de registos: " + LinhasAfetadas;
        }
Posted
Updated 2-Feb-17 7:24am
Comments
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

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).

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900