WinForm控件小知识
1.DataGridView控件显示自定义表
//造个数据表 DataTable dt = new DataTable(); dt.Columns.Add("DEcode", System.Type.GetType("System.String")); dt.Columns.Add("DEname", System.Type.GetType("System.String")); dt.Columns.Add("DEunit", System.Type.GetType("System.String")); dt.Columns.Add("DEliang", System.Type.GetType("System.Double")); dt.Columns.Add("DEprice", System.Type.GetType("System.Double")); dt.Columns.Add("DEtotal", System.Type.GetType("System.Double")); DataRow dr = dt.NewRow(); dr["DEcode"] = tab[0].Code; dr["DEname"] = tab[0].Name; dr["DEunit"] = tab[0].Unit; dr["DEliang"] = 1; dr["DEprice"] = tab[0].Price; dr["DEtotal"] = Convert.ToDouble(dr["DEliang"]) * Convert.ToDouble(dr["DEprice"]); dt.Rows.Add(dr); //不自动显示数据源中的列名 YuSuanBiaoDGV.AutoGenerateColumns = false; YuSuanBiaoDGV.DataSource = dt; //绑定每列的显示字段 YuSuanBiaoDGV.Columns[0].DataPropertyName = "DEcode"; YuSuanBiaoDGV.Columns[1].DataPropertyName = "DEname"; YuSuanBiaoDGV.Columns[2].DataPropertyName = "DEunit"; YuSuanBiaoDGV.Columns[3].DataPropertyName = "DEliang"; YuSuanBiaoDGV.Columns[4].DataPropertyName = "DEprice"; YuSuanBiaoDGV.Columns[5].DataPropertyName = "DEtotal"; YuSuanBiaoDGV.Columns[3].ReadOnly = false;
2. ListView显示自定义表
//造组,添加组 ListViewGroup ls = new ListViewGroup(); ls.Items.Add(buweiname); ls.Tag = buweicode; ls.Header = buweiname; YuSuanBiao.Groups.Add(ls); //小状态栏 GcbuweiState.Text = "当前工程部位:" + buweiname; //添加进预算表 ListViewItem item = new ListViewItem(); item.Text = "部位"; item.Tag = buweicode; item.Group = ls; item.BackColor = Color.LightPink; ListViewItem.ListViewSubItem dename = new ListViewItem.ListViewSubItem(); dename.Text = buweiname; item.SubItems.Add(dename); item.SubItems.Add("处"); ListViewItem.ListViewSubItem count = new ListViewItem.ListViewSubItem(); count.Text = "0"; item.SubItems.Add(count); ListViewItem.ListViewSubItem jiage = new ListViewItem.ListViewSubItem(); double jia = PartTotalPrice(GCname.Tag.ToString(), buweicode); jiage.Text = jia.ToString(); item.SubItems.Add(jiage); ListViewItem.ListViewSubItem zongjia = new ListViewItem.ListViewSubItem(); double zongjiage = Convert.ToDouble(count.Text) * Convert.ToDouble(jiage.Text); zongjia.Text = zongjiage.ToString(); item.SubItems.Add(zongjia); YuSuanBiao.Items.Add(item);
3. 修改DataGridView的列名显示:
string[] Htxt = { "定额号", "成本价", "定额名称", "定额单位代号", "档次代号", "工艺做法", "类型代号", "价格" }; for (int i = 0; i < DExiangxiDGV.Columns.Count; i++) { DExiangxiDGV.Columns[i].HeaderText = Htxt[i]; }