C# DataTable 详解
参考:https://www.cnblogs.com/Sandon/p/5175829.html
http://blog.csdn.net/singgel/article/details/51833116
1 namespace DXApplication1XtraTabControl 2 { 3 public partial class Form1 : DevExpress.XtraEditors.XtraForm 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 9 } 10 private DataTable CSUHInitDt() 11 { 12 DataTable csuhDt = new DataTable("CSUH"); 13 csuhDt.Columns.Add("下限", typeof(double)); 14 csuhDt.Columns.Add("上限", typeof(double)); 15 csuhDt.Columns.Add("精度", typeof(double)); 16 csuhDt.Rows.Add(new object [] { 0.50, 1.80, 0.01 }); 17 csuhDt.Rows.Add(new object[] { 0.010, 0.250, 0.001 }); 18 csuhDt.Rows.Add(new object[] { 0.01, 0.10, 0.01 }); 19 csuhDt.Rows.Add(new object[] { 0.01, 0.49, 0.01 }); 20 csuhDt.Rows.Add(new object[] { 0.20, 2.50, 0.01 }); 21 csuhDt.Rows.Add(new object[] { 0.10, 1.00, 0.01 }); 22 csuhDt.Rows.Add(new object[] { 0.1, 5.0, 0.1 }); 23 csuhDt.Rows.Add(new object[] { 0.10, 1.25, 0.01 }); 24 return csuhDt; 25 } 26 private void BindDataSource(DataTable dt) 27 { 28 //绑定DataTable 29 gridControl1.DataSource = dt; 30 //绑定DataSet 31 //gridControl1.DataSource = ds; 32 //gridControl1.DataMember = "表名"; 33 } 34 35 private void simpleButton1_Click(object sender, EventArgs e) 36 { 37 xtraTabControl1.SelectedTabPage = xtraTabPage2; 38 xtraTabControl3.SelectedTabPage = xtraTabPage7; 39 } 40 41 42 43 private void simpleButton2_Click(object sender, EventArgs e) 44 { 45 xtraTabControl1.SelectedTabPage = xtraTabPage5; 46 BindDataSource(CSUHInitDt()); 47 } 48 } 49 }
效果:
调试是在这个地方可以看到内容:
datatable-Rows-结果视图-[i]-ItemArray
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Xml; 10 11 namespace DXApplication1XtraTabControl 12 { 13 public partial class Form1 : DevExpress.XtraEditors.XtraForm 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 19 } 20 private DataTable CSUHInitDt() 21 { 22 DataTable csuhDt = new DataTable("CSUH"); 23 csuhDt.Columns.Add("参数", typeof(string)); 24 csuhDt.Columns.Add("下限", typeof(double)); 25 csuhDt.Columns.Add("上限", typeof(double)); 26 csuhDt.Columns.Add("精度", typeof(double)); 27 csuhDt.Rows.Add(new object[] { "M", 0.50, 1.80, 0.01 }); 28 csuhDt.Rows.Add(new object[] {"λ", 0.010, 0.250, 0.001 }); 29 csuhDt.Rows.Add(new object[] { "κ",0.01, 0.10, 0.01 }); 30 csuhDt.Rows.Add(new object[] { "ν",0.01, 0.49, 0.01 }); 31 csuhDt.Rows.Add(new object[] {"N", 0.20, 2.50, 0.01 }); 32 csuhDt.Rows.Add(new object[] { "χ",0.10, 1.00, 0.01 }); 33 csuhDt.Rows.Add(new object[] {"m", 0.1, 5.0, 0.1 }); 34 csuhDt.Rows.Add(new object[] {"Z", 0.10, 1.25, 0.01 }); 35 return csuhDt; 36 } 37 private void BindDataSource(DataTable dt) 38 { 39 //绑定DataTable 40 gridControl1.DataSource = dt; 41 gridView1.OptionsCustomization.AllowColumnMoving = false;//如何让各列头禁止移动 42 gridView1.OptionsCustomization.AllowFilter = false;//不允许过滤 43 gridView1.OptionsCustomization.AllowSort = false;//不允许排序 44 gridView1.OptionsCustomization.AllowGroup = false;//不允许分组 45 gridView1.OptionsView.ShowGroupPanel = false;//不显示组面板 46 gridView1.OptionsSelection.MultiSelect = true;//可以多选 47 gridView1.OptionsSelection.MultiSelectMode = 48 DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;//RowSelect; //选择模式MultiSelectMode = CheckBoxRowSelect, 49 gridView1.OptionsView.ColumnAutoWidth = true;//列宽自动 50 gridView1.OptionsCustomization.AllowColumnResizing = false;//不允许改变列宽 51 gridView1.OptionsBehavior.Editable = true;//允许编辑 52 gridView1.OptionsView.EnableAppearanceEvenRow = true;//设置奇、偶行交替颜色 53 gridView1.OptionsView.EnableAppearanceOddRow = true;//设置奇、偶行交替颜色 54 gridView1.Appearance.EvenRow.BackColor=Color.DeepSkyBlue; 55 gridView1.Appearance.OddRow.BackColor = Color.CornflowerBlue; 56 this.gridView1.IndicatorWidth = 30;//设置显示行号的列宽 57 MessageBox.Show((gridView1.GetDataRow(0)[0]).ToString ());//获取第二行第二列数据,下标从零开始 58 59 60 //.AllowFilter = False 61 //绑定DataSet 62 //gridControl1.DataSource = ds; 63 //gridControl1.DataMember = "表名"; 64 } 65 66 private void simpleButton1_Click(object sender, EventArgs e) 67 { 68 xtraTabControl1.SelectedTabPage = xtraTabPage2; 69 xtraTabControl3.SelectedTabPage = xtraTabPage7; 70 } 71 72 73 74 private void simpleButton2_Click(object sender, EventArgs e) 75 { 76 xtraTabControl1.SelectedTabPage = xtraTabPage5; 77 BindDataSource(CSUHInitDt()); 78 } 79 80 private void simpleButton3_Click(object sender, EventArgs e) 81 { 82 ////创建xmldoc的xml文档 83 //XmlDocument xmldoc = new XmlDocument(); 84 ////加入XML的声明段落:<?xmlversion="1.0" encoding="utf-8"?> 85 //XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); 86 //xmldoc.AppendChild(xmldecl); 87 ////创建根节点 88 //XmlElement xmlroot = xmldoc.CreateElement("user"); 89 //xmldoc.AppendChild(xmlroot); 90 ////创建根节点的子节点 91 //XmlElement ePerson = xmldoc.CreateElement("person"); 92 //ePerson.SetAttribute("name:姓名", "张三"); 93 //ePerson.SetAttribute("年龄", "21"); 94 //ePerson.SetAttribute("身高", "168"); 95 //xmlroot.AppendChild(ePerson); 96 97 //ePerson = xmldoc.CreateElement("person"); 98 //ePerson.SetAttribute("name:姓名", "李四"); 99 //ePerson.SetAttribute("年龄", "24"); 100 //ePerson.SetAttribute("身高", "172"); 101 //xmlroot.AppendChild(ePerson); 102 103 //ePerson = xmldoc.CreateElement("person"); 104 //ePerson.SetAttribute("name:姓名", "赵六"); 105 //ePerson.SetAttribute("年龄", "29"); 106 //ePerson.SetAttribute("身高", "178"); 107 //xmlroot.AppendChild(ePerson); 108 109 //ePerson = xmldoc.CreateElement("person"); 110 //ePerson.SetAttribute("姓名", "王麻子"); 111 //ePerson.SetAttribute("年龄", "25"); 112 //ePerson.SetAttribute("身高", "198"); 113 //xmlroot.AppendChild(ePerson); 114 //ePerson.Attributes["姓名"].Value = "猪猪";//直接就可以最后一个person的姓名改为“猪猪” 115 116 //创建xmldoc的xml文档 117 XmlDocument xmldoc = new XmlDocument(); 118 //加入XML的声明段落:<?xmlversion="1.0" encoding="utf-8"?> 119 XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); 120 xmldoc.AppendChild(xmldecl); 121 //创建根节点 122 XmlElement xmlroot = xmldoc.CreateElement("CSUH"); 123 xmldoc.AppendChild(xmlroot); 124 //创建根节点的子节点 125 XmlElement eParameter = xmldoc.CreateElement("M"); 126 eParameter.SetAttribute("下限", "0.50"); 127 eParameter.SetAttribute("上限", "1.80"); 128 eParameter.SetAttribute("步长", "0.01"); 129 xmlroot.AppendChild(eParameter); 130 131 eParameter = xmldoc.CreateElement("λ"); 132 eParameter.SetAttribute("下限", "0.010"); 133 eParameter.SetAttribute("上限", "0.250"); 134 eParameter.SetAttribute("步长", "0.001"); 135 xmlroot.AppendChild(eParameter); 136 137 eParameter = xmldoc.CreateElement("κ"); 138 eParameter.SetAttribute("下限", "0.01"); 139 eParameter.SetAttribute("上限", "0.10"); 140 eParameter.SetAttribute("步长", "0.01"); 141 xmlroot.AppendChild(eParameter); 142 143 eParameter = xmldoc.CreateElement("ν"); 144 eParameter.SetAttribute("下限", "0.01"); 145 eParameter.SetAttribute("上限", "0.49"); 146 eParameter.SetAttribute("步长", "0.01"); 147 xmlroot.AppendChild(eParameter); 148 149 eParameter = xmldoc.CreateElement("N"); 150 eParameter.SetAttribute("下限", "0.20"); 151 eParameter.SetAttribute("上限", "2.50"); 152 eParameter.SetAttribute("步长", "0.01"); 153 xmlroot.AppendChild(eParameter); 154 155 eParameter = xmldoc.CreateElement("χ"); 156 eParameter.SetAttribute("下限", "0.10"); 157 eParameter.SetAttribute("上限", "1.00"); 158 eParameter.SetAttribute("步长", "0.01"); 159 xmlroot.AppendChild(eParameter); 160 161 eParameter = xmldoc.CreateElement("m"); 162 eParameter.SetAttribute("下限", "0.1"); 163 eParameter.SetAttribute("上限", "5.0"); 164 eParameter.SetAttribute("步长", "0.1"); 165 xmlroot.AppendChild(eParameter); 166 167 eParameter = xmldoc.CreateElement("Z"); 168 eParameter.SetAttribute("下限", "0.10"); 169 eParameter.SetAttribute("上限", "1.25"); 170 eParameter.SetAttribute("步长", "0.01"); 171 xmlroot.AppendChild(eParameter); 172 173 174 ////设置属性以及文本节点 175 ////方法一 176 177 //person1.SetAttribute("name", "张三"); 178 ////方法二:这种方法可以用来获取和设置属性值,但前提是该属性要存在 179 //person2.Attributes["name"].Value = "张三三"; 180 181 182 183 184 //保存创建好的XML文档 185 xmldoc.Save(@"D:CSUH.xml"); 186 MessageBox.Show("xml输出完成!", "提醒"); 187 } 188 189 private void gridControl1_Click(object sender, EventArgs e) 190 { 191 //if (e.Info.IsRowIndicator && e.RowHandle >= 0) 192 //{ 193 // e.Info.DisplayText = (e.RowHandle + 1).ToString(); 194 //} 195 } 196 //显示行号 197 private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) 198 { 199 if (e.Info.IsRowIndicator && e.RowHandle >= 0) 200 { 201 e.Info.DisplayText = (e.RowHandle + 1).ToString(); 202 } 203 } 204 //单元的值变化的时候引起的事件 205 private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) 206 { 207 string s = string.Format("第{0}行,第{1}列,值改变为:{2:F5}。", e.RowHandle.ToString(), e.Column.VisibleIndex.ToString(), (e.Value).ToString()); 208 MessageBox.Show(s); 209 210 } 211 //得到GridControl单元格的值: string d =gridView1.GetRowCellDisplayText(i,col); i代表行号,col代表标题 212 213 214 215 } 216 217 218 }
效果如下:
https://jingyan.baidu.com/article/25648fc1d46dfb9191fd00c0.html
1 //绑定DataTable 2 gridControl1.DataSource = dt; 3 gridView1.OptionsCustomization.AllowColumnMoving = false;//如何让各列头禁止移动 4 gridView1.OptionsCustomization.AllowFilter = false;//不允许过滤 5 gridView1.OptionsCustomization.AllowSort = false;//不允许排序 6 gridView1.OptionsCustomization.AllowGroup = false;//不允许分组 7 gridView1.OptionsView.ShowGroupPanel = false;//不显示组面板 8 gridView1.OptionsSelection.MultiSelect = true;//可以多选
添加行号:
参考:http://www.cnblogs.com/liulunet/archive/2011/08/08/2131227.html
取第三行第三列单元格的值:
gridView2.GetDataRow(2)[2]
1 private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) 2 { 3 if (e.Info.IsRowIndicator && e.RowHandle >= 0) 4 { 5 e.Info.DisplayText = (e.RowHandle + 1).ToString(); 6 } 7 }
4.得到GridControl的列数
int a =gridView1.Columns.Count;
5.得到GridControl的行数
int b =gridView1.RowCount;
6.得到GridControl的列标题的值
DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns[0];第一个列表题
7.得到GridControl单元格的值:
string d =gridView1.GetRowCellDisplayText(i,col); i代表行号,col代表标题
string e =gridView1.GetFocusedRowCellValue("字段名");
DevExtreme视频教程集合 打开就可以看哦 http://www.360doc.com/content/14/0314/16/10893884_360567006.shtml
添加引用
using System.Data;
创建表
//创建一个空表 DataTable dt = new DataTable(); //创建一个名为"Table_New"的空表 DataTable dt = new DataTable("Table_New");
创建列
//1.创建空列 DataColumn dc = new DataColumn(); dt.Columns.Add(dc); //2.创建带列名和类型名的列(两种方式任选其一) dt.Columns.Add("column0", System.Type.GetType("System.String")); dt.Columns.Add("column0", typeof(String)); //3.通过列架构添加列 DataColumn dc = new DataColumn("column1",System.Type.GetType("System.DateTime")); DataColumn dc = new DataColumn("column1", typeof(DateTime)); dt.Columns.Add(dc);
创建行
//1.创建空行 DataRow dr = dt.NewRow(); dt.Rows.Add(dr); //2.创建空行 dt.Rows.Add(); //3.通过行框架创建并赋值 dt.Rows.Add("张三",DateTime.Now);//Add里面参数的数据顺序要和dt中的列的顺序对应
//4.通过复制dt2表的某一行来创建
dt.Rows.Add(dt2.Rows[i].ItemArray);
赋值和取值
//新建行的赋值 DataRow dr = dt.NewRow(); dr[0] = "张三";//通过索引赋值 dr["column1"] = DateTime.Now; //通过名称赋值 //对表已有行进行赋值 dt.Rows[0][0] = "张三"; //通过索引赋值 dt.Rows[0]["column1"] = DateTime.Now;//通过名称赋值 //取值 string name=dt.Rows[0][0].ToString(); string time=dt.Rows[0]["column1"].ToString();