功能简介:
界面展示:
DataGridView 显示界面设置
代码展示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
float cost = float.Parse(textBox4.Text.Trim());
label9.Text = Convert.ToString(cost);
if (cost >= sum)
{
cost -= sum;
label7.Text = Convert.ToString(cost);
}
else
MessageBox.Show("金额不足", "warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
int k = 0;
float sum = 0;
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "" || textBox1.Text.Trim() != null)
{
k++;
int rowIndex = dataGridView1.Rows.Add(1);
dataGridView1[0, rowIndex].Value = k;
dataGridView1[1, rowIndex].Value = textBox1.Text.Trim();
dataGridView1[2, rowIndex].Value = textBox2.Text.Trim();
dataGridView1[3, rowIndex].Value = textBox3.Text.Trim();
sum += float.Parse(textBox2.Text.Trim()) *
float.Parse(textBox3.Text.Trim());
label4.Text = Convert.ToString(sum);
}
}
}
问题1:
删除按钮代码:
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 1)
{
int rowIndex = dataGridView1.CurrentCell.RowIndex;
sum -= float.Parse(Convert.ToString( dataGridView1[2,rowIndex].Value)) *
float.Parse(Convert.ToString(dataGridView1[3, rowIndex].Value));
label4.Text = Convert.ToString(sum);
dataGridView1.Rows.RemoveAt(rowIndex);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
相同商品重组(暂未实现)
代码参考:
if (dataGridView1.Rows.Count > 1){
if (textBox1.Text.Trim() != "" || textBox1.Text.Trim() != null)
{
int cell = dataGridView1.Rows[1].Cells.Count;
for (int i = 0; i < cell; i++)
{
if (textBox1.Text.Trim() == dataGridView1[1, i].Value.ToString())
{
float f1 = float.Parse(Convert.ToString(dataGridView1[2, i].Value));
f1 += float.Parse(textBox2.Text.Trim());
dataGridView1[2, i].Value = f1;
float f2 = float.Parse(Convert.ToString(dataGridView1[3, i].Value));
f2 += float.Parse(textBox3.Text.Trim());
dataGridView1[3, i].Value = f2;
sum += float.Parse(textBox2.Text.Trim()) *
float.Parse(textBox3.Text.Trim());
label4.Text = Convert.ToString(sum);
}
}
}
}
关注视频号===>学更多网页设计,毕设不是问题