110,537
社区成员
发帖
与我相关
我的任务
分享
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
List<Food> sourceList = new List<Food>()
{
new Food{Name="直播鱼", Category=0},
new Food{Name="直大黄花鱼", Category=0},
new Food{Name="直富贵鱼", Category=0},
new Food{Name="带鱼", Category=1},
new Food{Name="大明虾", Category=1},
};
private void Form4_Load(object sender, EventArgs e)
{
this.dataGridView1.AutoGenerateColumns = false;
this.buttonCategory_Click(this.button1, e);
}
private void buttonCategory_Click(object sender, EventArgs e)
{
Button bt = sender as Button;
if (bt != null)
{
int category = Convert.ToInt32(bt.Tag);
this.dataGridView1.DataSource = sourceList.Where(x => x.Category == category).ToList();
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1 || e.ColumnIndex == 3)
{
var item = (Food)this.dataGridView1.Rows[e.RowIndex].DataBoundItem;
if (e.ColumnIndex == 1)
{
//加
item.Number++;
}
else
{
if (item.Number > 0)
{
//减
item.Number--;
}
}
this.dataGridView1.Refresh();
this.txtTotal.Text = sourceList.Sum(x => x.Number).ToString();
}
}
}
class Food
{
public string Name { get; set; }
public int Number { get; set; }
public int Category { get; set; }
}
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.label1 = new System.Windows.Forms.Label();
this.txtTotal = new System.Windows.Forms.TextBox();
this.Name1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column1 = new System.Windows.Forms.DataGridViewButtonColumn();
this.Number1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewButtonColumn();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(35, 45);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Tag = "0";
this.button1.Text = "活鲜";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.buttonCategory_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(35, 94);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Tag = "1";
this.button2.Text = "冰鲜";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.buttonCategory_Click);
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Name1,
this.Column1,
this.Number1,
this.Column2});
this.dataGridView1.Location = new System.Drawing.Point(141, 45);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(585, 267);
this.dataGridView1.TabIndex = 2;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(573, 355);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(47, 12);
this.label1.TabIndex = 3;
this.label1.Text = "总数量:";
//
// txtTotal
//
this.txtTotal.Location = new System.Drawing.Point(626, 352);
this.txtTotal.Name = "txtTotal";
this.txtTotal.ReadOnly = true;
this.txtTotal.Size = new System.Drawing.Size(100, 21);
this.txtTotal.TabIndex = 4;
//
// Name1
//
this.Name1.DataPropertyName = "Name";
this.Name1.HeaderText = "名称";
this.Name1.Name = "Name1";
this.Name1.ReadOnly = true;
//
// Column1
//
this.Column1.HeaderText = "";
this.Column1.Name = "Column1";
this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.Column1.Text = "+";
this.Column1.UseColumnTextForButtonValue = true;
this.Column1.Width = 20;
//
// Number1
//
this.Number1.DataPropertyName = "Number";
this.Number1.HeaderText = "数量";
this.Number1.Name = "Number1";
this.Number1.ReadOnly = true;
//
// Column2
//
this.Column2.HeaderText = "";
this.Column2.Name = "Column2";
this.Column2.Text = "-";
this.Column2.UseColumnTextForButtonValue = true;
this.Column2.Width = 20;
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(942, 497);
this.Controls.Add(this.txtTotal);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form4";
this.Text = "Form4";
this.Load += new System.EventHandler(this.Form4_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtTotal;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn Name1;
private System.Windows.Forms.DataGridViewButtonColumn Column1;
private System.Windows.Forms.DataGridViewTextBoxColumn Number1;
private System.Windows.Forms.DataGridViewButtonColumn Column2;
}