datagridview绑定数据
2015-01-27 11:31
267 查看
1.直接读取xml并将值赋给datagridview
xmlDoc.Load("deviceInfo.xml");
XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn;
int index = dataGridView1.Rows.Add();
DataGridViewRow newrow = dataGridView1.Rows[index];
newrow.Cells[0].Value = xe.ChildNodes.Item(0).InnerText;
newrow.Cells[1].Value = xe.ChildNodes.Item(1).InnerText;
newrow.Cells[2].Value = xe.ChildNodes.Item(2).InnerText;
newrow.Cells[3].Value = xe.ChildNodes.Item(3).InnerText;
newrow.Cells[4].Value = xe.ChildNodes.Item(4).InnerText;
newrow.Cells[5].Value = xe.ChildNodes.Item(5).InnerText;
newrow.Cells[6].Value = xe.ChildNodes.Item(6).InnerText;
newrow.Cells[7].Value = xe.ChildNodes.Item(7).InnerText;
newrow.Cells[8].Value = xe.ChildNodes.Item(8).InnerText;
newrow.Cells[9].Value = xe.ChildNodes.Item(9).InnerText;
newrow.Cells[10].Value = xe.ChildNodes.Item(10).InnerText;
newrow.Cells[11].Value = xe.ChildNodes.Item(11).InnerText;
newrow.Cells[12].Value = xe.ChildNodes.Item(12).InnerText;
newrow.Cells[13].Value = xe.ChildNodes.Item(13).InnerText;
newrow.Cells[14].Value = xe.ChildNodes.Item(14).InnerText;
newrow.Cells[15].Value = xe.ChildNodes.Item(15).InnerText;
newrow.Cells[16].Value = xe.ChildNodes.Item(16).InnerText;
newrow.Cells[17].Value = xe.ChildNodes.Item(17).InnerText;
newrow.Cells[18].Value = xe.ChildNodes.Item(18).InnerText;
newrow.Cells[19].Value = xe.ChildNodes.Item(19).InnerText;
}
2.给类中定义属性变量,读取xml的值,将每个节点的值存入BindingList中,并将其作为gridview的数据源
string addr, kind, num, mark ;
BindingList<Form1> bdlst = new BindingList<Form1>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("deviceInfo.xml");
XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes;
foreach (XmlNode xn in nodelist)
{
Form1 frm = new Form1();
frm.addr = xe.ChildNodes.Item(0).ToString();
frm.kind = xe.ChildNodes.Item(1).ToString();
frm.num = xe.ChildNodes.Item(2).ToString();
frm.mark = xe.ChildNodes.Item(3).ToString();
bdlst.Add(frm);*/
}
dataGridView1.DataSource = bdlst;
3.将xml数据存入dataset,通过dataset直接作为数据源展示
DataSet ds = new DataSet();
BindingSource bd = new BindingSource();
ds.ReadXml("deviceInfo.xml");
dataGridView1.Columns.Clear();
bd.DataSource = ds.Tables[0].DefaultView;
dataGridView1.DataSource = bd;
4. 将xml数据存入dataset,通过将dataset中的值一一取出赋给datagridview中的单元格
ds.ReadXml("deviceInfo.xml");
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(ds.Tables[0].Rows.Count);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i].ItemArray[j];
}
}
5.改变列标题,及列宽
dataGridView1.Columns[0].HeaderText = "1";
dataGridView1.Columns[0].Width = 20;
dataGridView1.Columns[1].HeaderText = "2";
dataGridView1.Columns[1].Width = 20;
dataGridView1.Columns[2].HeaderText = "3";
dataGridView1.Columns[2].Width = 20;
记录下来,希望对自己和需要的人有所帮助!
xmlDoc.Load("deviceInfo.xml");
XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn;
int index = dataGridView1.Rows.Add();
DataGridViewRow newrow = dataGridView1.Rows[index];
newrow.Cells[0].Value = xe.ChildNodes.Item(0).InnerText;
newrow.Cells[1].Value = xe.ChildNodes.Item(1).InnerText;
newrow.Cells[2].Value = xe.ChildNodes.Item(2).InnerText;
newrow.Cells[3].Value = xe.ChildNodes.Item(3).InnerText;
newrow.Cells[4].Value = xe.ChildNodes.Item(4).InnerText;
newrow.Cells[5].Value = xe.ChildNodes.Item(5).InnerText;
newrow.Cells[6].Value = xe.ChildNodes.Item(6).InnerText;
newrow.Cells[7].Value = xe.ChildNodes.Item(7).InnerText;
newrow.Cells[8].Value = xe.ChildNodes.Item(8).InnerText;
newrow.Cells[9].Value = xe.ChildNodes.Item(9).InnerText;
newrow.Cells[10].Value = xe.ChildNodes.Item(10).InnerText;
newrow.Cells[11].Value = xe.ChildNodes.Item(11).InnerText;
newrow.Cells[12].Value = xe.ChildNodes.Item(12).InnerText;
newrow.Cells[13].Value = xe.ChildNodes.Item(13).InnerText;
newrow.Cells[14].Value = xe.ChildNodes.Item(14).InnerText;
newrow.Cells[15].Value = xe.ChildNodes.Item(15).InnerText;
newrow.Cells[16].Value = xe.ChildNodes.Item(16).InnerText;
newrow.Cells[17].Value = xe.ChildNodes.Item(17).InnerText;
newrow.Cells[18].Value = xe.ChildNodes.Item(18).InnerText;
newrow.Cells[19].Value = xe.ChildNodes.Item(19).InnerText;
}
2.给类中定义属性变量,读取xml的值,将每个节点的值存入BindingList中,并将其作为gridview的数据源
string addr, kind, num, mark ;
BindingList<Form1> bdlst = new BindingList<Form1>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("deviceInfo.xml");
XmlNodeList nodelist = xmlDoc.SelectSingleNode("DEVICE").ChildNodes;
foreach (XmlNode xn in nodelist)
{
Form1 frm = new Form1();
frm.addr = xe.ChildNodes.Item(0).ToString();
frm.kind = xe.ChildNodes.Item(1).ToString();
frm.num = xe.ChildNodes.Item(2).ToString();
frm.mark = xe.ChildNodes.Item(3).ToString();
bdlst.Add(frm);*/
}
dataGridView1.DataSource = bdlst;
3.将xml数据存入dataset,通过dataset直接作为数据源展示
DataSet ds = new DataSet();
BindingSource bd = new BindingSource();
ds.ReadXml("deviceInfo.xml");
dataGridView1.Columns.Clear();
bd.DataSource = ds.Tables[0].DefaultView;
dataGridView1.DataSource = bd;
4. 将xml数据存入dataset,通过将dataset中的值一一取出赋给datagridview中的单元格
ds.ReadXml("deviceInfo.xml");
dataGridView1.Rows.Clear();
dataGridView1.Rows.Add(ds.Tables[0].Rows.Count);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
dataGridView1.Rows[i].Cells[j].Value = ds.Tables[0].Rows[i].ItemArray[j];
}
}
5.改变列标题,及列宽
dataGridView1.Columns[0].HeaderText = "1";
dataGridView1.Columns[0].Width = 20;
dataGridView1.Columns[1].HeaderText = "2";
dataGridView1.Columns[1].Width = 20;
dataGridView1.Columns[2].HeaderText = "3";
dataGridView1.Columns[2].Width = 20;
记录下来,希望对自己和需要的人有所帮助!
相关文章推荐
- ADO.NET学习:Access库的连接以及将数据绑定到DataGridView
- DataGridView中的当前行数据怎样自动绑定到TextBox
- 在数据绑定 DataGridView 控件时控制是否自动生成列
- 不能在数据绑定的 DataGridView 控件上设置 ColumnCount 属性
- datagridview数据绑定
- DataGridView数据绑定以及导出Excel示例
- DataGridView中绑定DataTable数据及相关操作实现代码
- DataGridView绑定数据到textbox
- [VB.NET]关于datagridview数据绑定菜鸟问题!!
- DataGridView中comboBox(DataGridViewComboBoxColumn)的数据绑定
- 地磅称量系统之(37~39) 直接向数据库的数据表WtBill添加测试数据以及绑定用户控件和使用编码的方式控制dataGridView控件的每列属性和添加数据数据库的表中不存在的字段(非绑定列)
- datagridview (三)DataGridViewComboBoxColumn 列或者DataGridViewComboBoxCell 赋值绑定数据的方法
- DataGridView 控件中轻松设置DataGridViewComboBoxColumn数据绑定属性。
- 将数据绑定到 Windows 窗体 DataGridView 控件
- DataGridView绑定数据时,时间格式显示不全的问题
- DataGridView 数据绑定的一般过程
- vs2005 winfoms DataGridView DataGridViewColumn 数据绑定
- DataGridView绑定数据后列的显示次序随机改变的解决方案
- 如何在已经绑定数据的dataGridView对象中动态添加记录?
- DataGridView中绑定DataTable数据及相关操作