Retrieve Date From Database To Datagridview Combobox Columns

50 views Asked by At

I made a datagridview with 3 combobox cells 0 , 1 , 2 and 4 another cells as textbox the problem now its combo box never accept values from database textbox cells accept I don't know why I felt boring two days without solution here is the following code that i use on textchange event :

private void textBox1_TextChanged(object sender, EventArgs e)
{    
    int dataRecordsCount = 0;
    try
    {
        //Select branch data where brnch id 
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            string countDdQuery = 
            @"SELECT 
                COUNT(*) 
            FROM DisposeData  
            WHERE
            dispose_id = @dispose_id AND branch_id=@branch_id";
            using (SqlCommand command = new SqlCommand(countDdQuery, connection))
            {
                command.Parameters.AddWithValue("@dispose_id", textBox1.Text);
                command.Parameters.AddWithValue("@branch_id", 1);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dataRecordsCount = Convert.ToInt16(reader[0]);
                    }
                }
            }
            string query = 
            @" SELECT 
                d.dispose_id, 
                c.catg_id, 
                c.catgNameAr, 
                u.unit_NameAr, 
                dd.Qty, dd.Price, 
                dd.Discount, 
                dd.Total
            FROM Dispose AS d
            INNER JOIN 
                DisposeData AS dd 
                ON d.dispose_id = dd.dispose_id
            INNER JOIN 
                Categories AS c 
                ON dd.dcatgId = c.catg_id
            INNER JOIN 
                Units AS u 
                ON c.mainUnitId = u.unit_id where d.dispose_id = @dispose_id";
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                command.Parameters.AddWithValue("@dispose_id", textBox1.Text);   

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read()) // Check if there are rows
                    {
                        textBox1.Text = reader["dispose_id"].ToString(); 

                        
                        if (dataRecordsCount == 1)
                        {
                            grid1.Rows.Add();
                            grid1.Rows[0].Cells[0].Value 
                            = reader["catg_id"].ToString();
                            grid1.Rows[0].Cells[1].Value 
                            = reader["catgNameAr"].ToString();
                            grid1.Rows[0].Cells[2].Value 
                            = reader["unit_NameAr"].ToString();
                            grid1.Rows[0].Cells[3].Value 
                            = reader["Qty"].ToString();

                            decimal price = Convert.ToDecimal(reader["Price"]);
                            grid1.Rows[0].Cells[4].Value = price.ToString("0.00");

                            decimal Discount = Convert.ToDecimal(reader["Discount"]);
                            grid1.Rows[0].Cells[5].Value = Discount.ToString("0.00");

                            decimal Total = Convert.ToDecimal(reader["Total"]);
                            grid1.Rows[0].Cells[6].Value = Total.ToString("0.00");
                            grid1.Refresh();
                        }
                        else
                        {
                            for (int i = 1; i <= dataRecordsCount; i++)
                            {
                                grid1.Rows.Add();
                                grid1.Rows[i].Cells[0].Value 
                                = reader["catg_id"].ToString();
                                grid1.Rows[i].Cells[1].Value 
                                = reader["catgNameAr"].ToString();
                                grid1.Rows[i].Cells[2].Value 
                                = reader["unit_NameAr"].ToString();
                                grid1.Rows[i].Cells[3].Value 
                                = reader["Qty"].ToString();

                                decimal price = Convert.ToDecimal(reader["Price"]);
                                grid1.Rows[0].Cells[4].Value = price.ToString("0.00");

                                decimal Discount = Convert.ToDecimal(reader["Discount"]);
                                grid1.Rows[0].Cells[5].Value = Discount.ToString("0.00");

                                decimal Total = Convert.ToDecimal(reader["Total"]);
                                grid1.Rows[0].Cells[6].Value = Total.ToString("0.00");

                            }
                        }                                                        
                    }
                }
            }
        }
    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }
}

I tried the previous code i mentioned it above but no result the three cells 0,1,2 are empty and cells 3,4,5,6 bring the data

0

There are 0 answers