在winform開辟過程中,需要對datagridview中查詢出來的數據進行計較,也就是說,我們在此中一個欄位中輸入數據,將會使指心猿意馬的單位格數據也發生轉變,下面,我們就一路來看看,怎么實現輸入重量,按照原有的單價和數目算出總金額。
在winform中把要實現的功能界面先做好,這里,我但愿經由過程輸入重量來計較出總金額。
編寫根基數據抓取的代碼
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && textBox1.Text != "")
{
show_data(dataGridView1);
//dataGridView1.AllowUserToAddRows = false;//關閉dataGridview中最後一個空白行。
}
else
{
}
}
用datagridview中單位格編纂完當作的事務去做實現數據的變動和后續動作。
編寫單位格編纂完當作后的事務代碼
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (int.Parse(dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString()) > 0)
{
float m = float.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString());
float l = float.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString());
int n = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString());
string a = ((m * n)+(l*n)).ToString();
this.dataGridView1.Rows[e.RowIndex].Cells[11].Value = a;
}
}
}
else
{
}
}
注重重點行號必然是要用 e.RowIndex來暗示,如許才會輪回到最后一行,不然會報錯。
測試輸入重量按照設心猿意馬的公式顯示總金額,目標達到。
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!