У меня два одинаковых DataGridView
, оба имеют одинаковые Properties
и одинаковые Events
.Но один из них не меняет значение, когда пользователь меняет значение внутри DataGridView
.
Это мои DataGridView
события
DataGridView dgvselect;
Label totAmt;
Label totItem;
private void dataGridRight_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (tabRight.SelectedTab == tabCart)
{
dgvselect = dataGridSales;
totAmt = lblSTotAmt;
totItem = lblSTotItem;
dataGridSales.BeginEdit(true);
}
if (tabRight.SelectedTab == tabEsti)
{
dgvselect = dataGridEsti;
totAmt = lblETotAmt;
totItem = lblETotItem;
dataGridEsti.BeginEdit(true);
}
}
private void dataGridRight_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int lastqty = Convert.ToInt32(dgvselect.SelectedRows[0].Cells[6].Value.ToString());
int lessqty = Convert.ToInt32(dgvselect.SelectedRows[0].Cells[1].Value.ToString());
string part = dgvselect.SelectedRows[0].Cells[5].Value.ToString();
int nqty = lastqty - lessqty;
if (lessqty <= lastqty)
{
using (SqlConnection conn = new SqlConnection(@"Server=" + ip + "," + port + "; Database=records; User ID=" + sqlid + "; Password=" + sqlpass + ""))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[products]
SET Stock = '" + nqty + "' WHERE [Part No.] = '" + part + "'", conn))
{
cmd.ExecuteNonQuery();
}
conn.Close();
}
scrollIndex = dataGridSrch.SelectedRows[0].Index;
scrollPos = dataGridSrch.FirstDisplayedScrollingRowIndex;
searchProd();
if (e.ColumnIndex == 1)
{
int qty = Convert.ToInt32(dgvselect.Rows[e.RowIndex].Cells[1].Value);
double price = Convert.ToDouble(dgvselect.Rows[e.RowIndex].Cells[4].Value);
dgvselect.Rows[e.RowIndex].Cells[3].Value = qty * price;
}
if (e.ColumnIndex == 3)
{
double price = Convert.ToDouble(dgvselect.Rows[e.RowIndex].Cells[3].Value);
dgvselect.Columns[3].ValueType = typeof(Double);
}
dgvselect.Columns[3].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-PH");
dgvselect.Columns[3].DefaultCellStyle.Format = String.Format("C");
}
else
{
MessageBox.Show("Exceeds the amount of stock available", "Invalid Purchase", MessageBoxButtons.OK);
dgvselect.Rows[e.RowIndex].Cells[1].Value = 1;
}
}
private void dataGridRight_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (tabRight.SelectedTab == tabSales || tabRight.SelectedTab == tabEsti)
{
int count = 0;
double total = 0;
if (e.ColumnIndex == 1)
{
foreach (DataGridViewRow row in dgvselect.Rows)
{
count += Convert.ToInt32(row.Cells[1].Value);
}
totItem.Text = "Total Items: " + count;
}
if (e.ColumnIndex == 3)
{
foreach (DataGridViewRow row in dgvselect.Rows)
{
total += Convert.ToDouble(row.Cells[3].Value);
}
totAmt.Text = "Total Amount: " + total.ToString("C2");
}
amount = total;
}
}
Когда пользователь дважды щелкает ячейку,он начнет редактирование и установит значения dgvselect
, totAmt
и totItem
для соответствующих им DataGridView
и Labels
Но при тестировании приложения вкладка Cart
нестрельба.
Это начальные значения, когда элемент добавляется в DataGridView
.Для Qty
и Price
.

изменения не вносятся, но при некоторых изменениях

Тексты totAmt
и totItem
не изменились.Он будет меняться только при добавлении элемента.

Что касается вкладки Estimate
, он может изменять текстовые значения totAmt
и totItem
.


Я попытался скопировать DataGridView
вEstimate
закладку и вставьте ее в Cart
закладку.