码迷,mamicode.com
首页 > Windows程序 > 详细

【毕设】关于ComboBox与DataGridView的用法

时间:2017-02-28 15:33:30      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:ade   lin   循环   add   isp   ble   datatable   tab   string   

ComboBox的用法:

1.ComboBox绑定DataTable

DataTable dt = _product.GetCode(cbbCategory.Text);//声明DataTabble对象并赋值
ComboBox1.DataSource = dt;//绑定DataTable
ComboBox1.DisplayMember = dt.Columns["code"].ToString();//设置要显示的DataTable的列

2.直接使用Add()添加

foreach(string col in color)
{
     ComboBox1.Items.Add(col);
}

DataGridView的用法:

1.动态生成行与列,并给行头列头命名:

string[] colors = product.GetColor(code);//颜色数组作为行
string[] sizes = product.GetSize(code);//尺码数组作为列
 if (colors == null || sizes == null) return;
dgvPurchase.ColumnCount = sizes.Length;//设置列数
 for (int i = 0; i < sizes.Length; i++)
{
     dgvPurchase.Columns[i].Name = sizes[i];//循环给列头命名
}
dgvPurchase.RowCount = colors.Length;//设置行数
 for (int j = 0; j < colors.Length; j++)
{
dgvPurchase.Rows[j].HeaderCell.Value = colors[j];//循环给行头命名
}

2.向DataGridView中插入一整行数据:

DataGridViewRow dr = new DataGridViewRow();//声明行
dr.CreateCells(dgvProduct);
dr.Cells[0].Value = strs[0];//添加元素
dr.Cells[1].Value = strs[1];
dr.Cells[2].Value = strs[2];
dr.Cells[3].Value = strs[3];
dr.Cells[4].Value = strs[4];
dr.Cells[5].Value = strs[5];
dr.Cells[6].Value = strs[6];
dgvProduct.Rows.Add(dr);//插入行

3.遍历DataGridView同理

foreach(DataGridViewRow dr in DataGridView1.Rows)
{
    for(int i=0;i<dr.Cells.Count;i++)
    {
       str[i]=dr[i].Value.ToString();//提取元素
    }
}

 

【毕设】关于ComboBox与DataGridView的用法

标签:ade   lin   循环   add   isp   ble   datatable   tab   string   

原文地址:http://www.cnblogs.com/DAantony/p/6478710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!