Search code examples
c#sqldatagridviewkeypress

Get data from DB to datagridview based on datagrid values C#


I have a question and I don't seem to get it right in this one... What I've been trying to do is enter a value (id) on a datagridview cell, pressing enter, and filling the adjacents cells with data from the DB (SQL), that matches the id that I entered in the first cell. The questions are:

  1. How do I get the pressed key event on a datagridview?

  2. How do I get the value from that particular cell into, let's an integer?

  3. How do I add up rows from a datatable, without getting the first row deleted in the datagridview? Is there an update method to it?

Sorry if these are basic questions, but I don't seem to find an answer. Thanks!

Edit: Here's the code I've been trying... what I accomplished with this is that when I press enter, I get a set of new empty columns on the datagrid aside of the columns that I already created

private void dataGridView_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCell cell = row.Cells[0];
                if (cell.Value == null || cell.Value.Equals("") || cell.ColumnIndex == 0)
                {
                    dataGridView1.CurrentRow.cell[0].FormattedValue as int;
                    int idArticle = Convert.ToInt32(row.Cells[0].Value);
                    //int idArticle = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
                    dataGridView1.AutoGenerateColumns = true;
                    string constring = @"Data Source=DAN;Initial Catalog=administration;Integrated Security=True ";
                    using (SqlConnection con = new SqlConnection(constring))
                    {
                        using (SqlCommand cmd = new SqlCommand("SELECT id_article, article, quantity, precio FROM articlesSG WHERE id_article=@id_article", con))
                        {
                            cmd.Parameters.AddWithValue("id_article", idArticle);
                            cmd.CommandType = CommandType.Text;
                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                            {
                                using (DataTable dt = new DataTable())
                                {
                                    sda.Fill(dt);
                                    dataGridView1.DataSource = dt;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

Solution

  • To capture keys try this:

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
      var tb =(DataGridViewTextBoxEditingControl)e.Control;
      tb.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
    
      e.Control.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
    }
    
    private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    }
    

    To get the current value :

    dataGridView1.CurrentRow.Cell[indexorname].FormattedValue as int
    

    To add up rows use DataTable.Merge: