Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
down vote
favorite
I have datatable with 3 columns Colorname Sizename Barcode

and datagridview 3 columns names
Color size barcode

I want to retrieve data from database using datatable and insert into datagridview column names

I know datagridview.DataSource=dt;

but it show me all data with database column names, but I want insert data into datagridview column names.


What I have tried:

\\Save Qurey....

            ColorTextBox.Focus();
            string colorname;
            colorname = ColorTextBox.Text;
            ColorTextBox.Clear();
            //dataGridView2.Columns.Clear();
            //SearchTextBox.Text = "  ";

            //SearchTextBox.Clear();




//s_________________________________________sTART_________________________________

            //int temp = 1;
            int temp2;
            int result1;




            //result1 = temp + temp2;
            //textBox9.Text = result1.ToString();




            //textBox4.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();






            for (int a = int.Parse(SizeFromTextBox.Text); a <= int.Parse(SizeToTextBox.Text); a = a + 1)
            {
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter("Get_Barcode_First", con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);

                con.Close();

                temp2 = Convert.ToInt32(dt.Rows[0][0].ToString());

                result1 = a + temp2;

                int row = 0;
                dataGridView3.Rows.Add();
                row = dataGridView3.Rows.Count - 2;
                dataGridView3["color", row].Value = colorname;
                dataGridView3["Size", row].Value = a;
                dataGridView3["Barcode", row].Value = result1;

            }


            //int lastRow = dataGridView1.Rows.Count - 1;
            //object last_order_ID = dataGridView1.Rows[lastRow].Cells["Barcode"].Value;
            //textBox4.Text = Convert.ToString(last_order_ID);
            //int vdff = dataGridView1.NewRowIndex;
            //textBox4.Text = vdff.ToString();












            for (int a = 0; a < dataGridView3.Rows.Count - 1; a++)
            {
                //con.Open();
                //SqlCommand cmd = new SqlCommand("delete  from ItemRegBarcode where barcode = 1 ", con);
                //cmd.Parameters.AddWithValue("@barcode", dataGridView1.Rows[a].Cells[2].Value);
                //cmd.ExecuteNonQuery();
                //con.Close();

                textBox1.Text = dataGridView3.Rows[a].Cells[2].Value.ToString();


                string query = "select * from ItemRegBarcode where Barcode='" + textBox1.Text + "'";
                SqlDataAdapter sda = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);



                if (dt.Rows.Count == 1)
                {


                    //for (int a = 0; a < dataGridView1.Rows.Count - 1; a++)
                    //{

                    textBox1.Text = dataGridView3.Rows[a].Cells[2].Value.ToString();
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  from ItemRegBarcode where barcode = '" + textBox1.Text + "' ", con);
                    cmd.Parameters.AddWithValue("@barcode", dataGridView3.Rows[a].Cells[2].Value);
                    cmd.ExecuteNonQuery();
                    con.Close();

                    //}







                }
                con.Open();
                SqlCommand cmd1 = new SqlCommand("insert into ItemRegBarcode (itemcode,date,barcode,colorname,sizename) values('" + ItemIdTextBox.Text + "','" + dateTimePicker1.Value.ToString("MM-dd-yyyy") + "',@barcode,@color,@size) ", con);
                cmd1.Parameters.AddWithValue("@barcode", dataGridView3.Rows[a].Cells[2].Value);
                cmd1.Parameters.AddWithValue("@color", dataGridView3.Rows[a].Cells[0].Value);
                cmd1.Parameters.AddWithValue("@size", dataGridView3.Rows[a].Cells[1].Value);
                cmd1.ExecuteNonQuery();
                con.Close();






            }


i try this
string query = "select colorname,sizename,barcode from ItemRegBarcode where itemcode='" + ItemIdTextBox.Text + "' ";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
sda.Fill(dt1);




for (int i = 0; i < dt1.Columns.Count; i++)
{
    int row = 0;

    dataGridView3.Rows.Add();
    row = dataGridView3.Rows.Count - 2;
    //    dataGridView3["color", row].Value = dt1.Columns.Add("colorname").ToString();
    dataGridView3["color", 0].Value = dt1.Rows[i]["ColorName"].ToString();
}
Posted
Updated 16-Jan-17 23:12pm
Comments
Sunasara Imdadhusen 17-Jan-17 5:10am    
Not clear! don't put entire code, provide only that code which may cause the issue.

1 solution

You can add direct DataTable to DataGridView source. refer below example:
C#
DataTable dt1 = new DataTable();
dataGridView3.DataSource = dt1; 
 
Share this answer
 
Comments
Member 12861580 17-Jan-17 5:43am    
i have already column names of datagridview which i insert the datatable datasource insert tables colmn names i want to already difined column names in datagridview

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