I am currently using a datgridview1 on form1 that allows the user to select by checking on checkboxcolumn. These are then copied to form 2 datagridview1 which later is exported to my excel template. This works great.
The only problem is that the default value on "qty" is "0". The user has to enter an amount and then select the checkboxcolumn. Is it possible for the checkboxcolumn to automatically get checked once a qty on a product is entered excluding the "0" values? At times the user has to enter a qty amount and then check. This can sometimes be 80 times. This takes a lot of time.
Also at the moment if the checkbox is checked first and then the qty is changed it does not carry the changed qty value to form2 on button click but the "0" value.
Alternatively i would prefer no checkbox and if qty is entered then only that will be carried to form2
public Test()
{
sqlite = new SQLiteConnection("Data Source=C:/MyData/MyItem.db;");
InitializeComponent();
DataColumnCollection columns = Test.dt.Columns;
DataColumn[] dataColumn = new DataColumn[] { new DataColumn("ProdCode"), new DataColumn("Product"), new DataColumn("Pack"), new DataColumn("t1"), new DataColumn("t2"), new DataColumn("Qty"), new DataColumn("WSale") };
columns.AddRange(dataColumn);
}
private void BindDataGrid(string query)
{
this.dataGridView1.DataSource = null;
this.dataGridView1.Columns.Clear();
this.dataGridView1.ColumnCount = 7;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.Columns[0].Name = "ProdCode";
this.dataGridView1.Columns[0].HeaderText = "ProdCode";
this.dataGridView1.Columns[0].DataPropertyName = "ProdCode";
this.dataGridView1.Columns[0].Width = 100;
this.dataGridView1.Columns[1].HeaderText = "Product";
this.dataGridView1.Columns[1].Name = "Product";
this.dataGridView1.Columns[1].DataPropertyName = "Product";
this.dataGridView1.Columns[1].Width = 160;
this.dataGridView1.Columns[2].HeaderText = "t1";
this.dataGridView1.Columns[2].Name = "t1";
this.dataGridView1.Columns[2].DataPropertyName = "t1";
this.dataGridView1.Columns[2].Width = 1;
this.dataGridView1.Columns[2].Visible = false;
this.dataGridView1.Columns[3].HeaderText = "Pack";
this.dataGridView1.Columns[3].Name = "Pack";
this.dataGridView1.Columns[3].DataPropertyName = "Pack";
this.dataGridView1.Columns[3].Width = 50;
this.dataGridView1.Columns[4].HeaderText = "t2";
this.dataGridView1.Columns[4].Name = "t2";
this.dataGridView1.Columns[4].DataPropertyName = "t2";
this.dataGridView1.Columns[4].Width = 1;
this.dataGridView1.Columns[4].Visible = false;
this.dataGridView1.Columns[5].Name = "Qty";
this.dataGridView1.Columns[5].HeaderText = "Qty";
this.dataGridView1.Columns[5].DataPropertyName = "Qty";
this.dataGridView1.Columns[5].Width = 50;
this.dataGridView1.Columns[6].Name = "WSale";
this.dataGridView1.Columns[6].HeaderText = "WSale";
this.dataGridView1.Columns[6].DataPropertyName = "WSale";
this.dataGridView1.Columns[6].Width = 58;
this.dataGridView1.Columns[6].DefaultCellStyle.Format = "0.00##";
DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn()
{
HeaderText = "",
Width = 30,
Name = "checkBoxColumn"
};
dataGridView1.Columns.Insert(0, dataGridViewCheckBoxColumn);
using (SQLiteConnection con = new SQLiteConnection(sqlite))
{
using (SQLiteCommand cmd = new SQLiteCommand(query, con))
{
cmd.CommandType = CommandType.Text;
using (SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
}
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string str1 = row.Cells[1].Value.ToString();
string str2 = row.Cells[2].Value.ToString().ToLower();
string str3 = row.Cells[3].Value.ToString();
string str4 = row.Cells[4].Value.ToString();
string str5 = row.Cells[5].Value.ToString();
string str6 = row.Cells[6].Value.ToString();
string str7 = row.Cells[7].Value.ToString();
//string style = "<style>.UppercaseWords</style>";
foreach (DataRow dr in dt.Rows)
{
string str8 = dr["ProdCode"].ToString();
string str9 = dr["Product"].ToString().ToLower();
string str10 = dr["t1"].ToString();
string str11 = dr["Pack"].ToString();
string str12 = dr["t2"].ToString();
string str13 = dr["Qty"].ToString();
string str14 = dr["WSale"].ToString();
if (str1 == str8 && str2 == str9 && str3 == str10 && str4 == str11 && str5 == str12 && str6 == str13 && str7 == str14) { }
{
row.Cells["checkBoxColumn"].Value = true;
}
}
}
private void vButton1_Click(object sender, EventArgs e)
{
DataView dw = new DataView(dt);
string[] cols = { "ProdCode", "Product", "t1", "t2", "Pack", "Qty", "WSale" };
DataTable dt1 = dw.ToTable(true, cols);
if (dt1.Rows.Count > 0)
{
Form2 f = new Form2(dt1);
f.ShowDialog();
}
else
{
MessageBox.Show("Select atleast one record");
}
}
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
string value4 = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].EditedFormattedValue.ToString().ToLower() == "true")
{
dt.Rows.Add(dataGridView1.Rows[e.RowIndex].Cells[1].Value, UppercaseWords(value4), dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value, dataGridView1.Rows[e.RowIndex].Cells[5].Value, dataGridView1.Rows[e.RowIndex].Cells[6].Value, dataGridView1.Rows[e.RowIndex].Cells[7].Value);
}
else
{
DataRow[] dataRows = dt.Select(string.Format("ProdCode='{0}' and Product='{1}' and t1='{2}' and t2='{3}' and Pack='{4}' and Qty='{5}' and WSale='{6}'", dataGridView1.Rows[e.RowIndex].Cells[1].Value, dataGridView1.Rows[e.RowIndex].Cells[2].Value, dataGridView1.Rows[e.RowIndex].Cells[3].Value, dataGridView1.Rows[e.RowIndex].Cells[4].Value, dataGridView1.Rows[e.RowIndex].Cells[5].Value, dataGridView1.Rows[e.RowIndex].Cells[6].Value, dataGridView1.Rows[e.RowIndex].Cells[7].Value));
foreach (DataRow dataRow in dataRows)
{
dt.Rows.Remove(dataRow);
}
}
}