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();
int temp2;
int result1;
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;
}
for (int a = 0; a < dataGridView3.Rows.Count - 1; a++)
{
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)
{
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", 0].Value = dt1.Rows[i]["ColorName"].ToString();
}