c# 数据表DataTable给devexpress的gridControl提供数据源

C# DataTable 详解

参考:https://www.cnblogs.com/Sandon/p/5175829.html

http://blog.csdn.net/singgel/article/details/51833116

 namespace DXApplication1XtraTabControl
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent(); }
private DataTable CSUHInitDt()
{
DataTable csuhDt = new DataTable("CSUH");
csuhDt.Columns.Add("下限", typeof(double));
csuhDt.Columns.Add("上限", typeof(double));
csuhDt.Columns.Add("精度", typeof(double));
csuhDt.Rows.Add(new object [] { 0.50, 1.80, 0.01 });
csuhDt.Rows.Add(new object[] { 0.010, 0.250, 0.001 });
csuhDt.Rows.Add(new object[] { 0.01, 0.10, 0.01 });
csuhDt.Rows.Add(new object[] { 0.01, 0.49, 0.01 });
csuhDt.Rows.Add(new object[] { 0.20, 2.50, 0.01 });
csuhDt.Rows.Add(new object[] { 0.10, 1.00, 0.01 });
csuhDt.Rows.Add(new object[] { 0.1, 5.0, 0.1 });
csuhDt.Rows.Add(new object[] { 0.10, 1.25, 0.01 });
return csuhDt;
}
private void BindDataSource(DataTable dt)
{
//绑定DataTable
gridControl1.DataSource = dt;
//绑定DataSet
//gridControl1.DataSource = ds;
//gridControl1.DataMember = "表名";
} private void simpleButton1_Click(object sender, EventArgs e)
{
xtraTabControl1.SelectedTabPage = xtraTabPage2;
xtraTabControl3.SelectedTabPage = xtraTabPage7;
} private void simpleButton2_Click(object sender, EventArgs e)
{
xtraTabControl1.SelectedTabPage = xtraTabPage5;
BindDataSource(CSUHInitDt());
}
}
}

CSUH本构模型上下限

效果:

c# 数据表DataTable给devexpress的gridControl提供数据源

调试是在这个地方可以看到内容:

c# 数据表DataTable给devexpress的gridControl提供数据源

c# 数据表DataTable给devexpress的gridControl提供数据源

c# 数据表DataTable给devexpress的gridControl提供数据源

datatable-Rows-结果视图-[i]-ItemArray

c# 数据表DataTable给devexpress的gridControl提供数据源


 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml; namespace DXApplication1XtraTabControl
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent(); }
private DataTable CSUHInitDt()
{
DataTable csuhDt = new DataTable("CSUH");
csuhDt.Columns.Add("参数", typeof(string));
csuhDt.Columns.Add("下限", typeof(double));
csuhDt.Columns.Add("上限", typeof(double));
csuhDt.Columns.Add("精度", typeof(double));
csuhDt.Rows.Add(new object[] { "M", 0.50, 1.80, 0.01 });
csuhDt.Rows.Add(new object[] {"λ", 0.010, 0.250, 0.001 });
csuhDt.Rows.Add(new object[] { "κ",0.01, 0.10, 0.01 });
csuhDt.Rows.Add(new object[] { "ν",0.01, 0.49, 0.01 });
csuhDt.Rows.Add(new object[] {"N", 0.20, 2.50, 0.01 });
csuhDt.Rows.Add(new object[] { "χ",0.10, 1.00, 0.01 });
csuhDt.Rows.Add(new object[] {"m", 0.1, 5.0, 0.1 });
csuhDt.Rows.Add(new object[] {"Z", 0.10, 1.25, 0.01 });
return csuhDt;
}
private void BindDataSource(DataTable dt)
{
//绑定DataTable
gridControl1.DataSource = dt;
gridView1.OptionsCustomization.AllowColumnMoving = false;//如何让各列头禁止移动
gridView1.OptionsCustomization.AllowFilter = false;//不允许过滤
gridView1.OptionsCustomization.AllowSort = false;//不允许排序
gridView1.OptionsCustomization.AllowGroup = false;//不允许分组
gridView1.OptionsView.ShowGroupPanel = false;//不显示组面板
gridView1.OptionsSelection.MultiSelect = true;//可以多选
gridView1.OptionsSelection.MultiSelectMode =
DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;//RowSelect; //选择模式MultiSelectMode = CheckBoxRowSelect,
gridView1.OptionsView.ColumnAutoWidth = true;//列宽自动
gridView1.OptionsCustomization.AllowColumnResizing = false;//不允许改变列宽
gridView1.OptionsBehavior.Editable = true;//允许编辑
gridView1.OptionsView.EnableAppearanceEvenRow = true;//设置奇、偶行交替颜色
gridView1.OptionsView.EnableAppearanceOddRow = true;//设置奇、偶行交替颜色
gridView1.Appearance.EvenRow.BackColor=Color.DeepSkyBlue;
gridView1.Appearance.OddRow.BackColor = Color.CornflowerBlue;
this.gridView1.IndicatorWidth = ;//设置显示行号的列宽
MessageBox.Show((gridView1.GetDataRow()[]).ToString ());//获取第二行第二列数据,下标从零开始 //.AllowFilter = False
//绑定DataSet
//gridControl1.DataSource = ds;
//gridControl1.DataMember = "表名";
} private void simpleButton1_Click(object sender, EventArgs e)
{
xtraTabControl1.SelectedTabPage = xtraTabPage2;
xtraTabControl3.SelectedTabPage = xtraTabPage7;
} private void simpleButton2_Click(object sender, EventArgs e)
{
xtraTabControl1.SelectedTabPage = xtraTabPage5;
BindDataSource(CSUHInitDt());
} private void simpleButton3_Click(object sender, EventArgs e)
{
////创建xmldoc的xml文档
//XmlDocument xmldoc = new XmlDocument();
////加入XML的声明段落:<?xmlversion="1.0" encoding="utf-8"?>
//XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
//xmldoc.AppendChild(xmldecl);
////创建根节点
//XmlElement xmlroot = xmldoc.CreateElement("user");
//xmldoc.AppendChild(xmlroot);
////创建根节点的子节点
//XmlElement ePerson = xmldoc.CreateElement("person");
//ePerson.SetAttribute("name:姓名", "张三");
//ePerson.SetAttribute("年龄", "21");
//ePerson.SetAttribute("身高", "168");
//xmlroot.AppendChild(ePerson); //ePerson = xmldoc.CreateElement("person");
//ePerson.SetAttribute("name:姓名", "李四");
//ePerson.SetAttribute("年龄", "24");
//ePerson.SetAttribute("身高", "172");
//xmlroot.AppendChild(ePerson); //ePerson = xmldoc.CreateElement("person");
//ePerson.SetAttribute("name:姓名", "赵六");
//ePerson.SetAttribute("年龄", "29");
//ePerson.SetAttribute("身高", "178");
//xmlroot.AppendChild(ePerson); //ePerson = xmldoc.CreateElement("person");
//ePerson.SetAttribute("姓名", "王麻子");
//ePerson.SetAttribute("年龄", "25");
//ePerson.SetAttribute("身高", "198");
//xmlroot.AppendChild(ePerson);
//ePerson.Attributes["姓名"].Value = "猪猪";//直接就可以最后一个person的姓名改为“猪猪” //创建xmldoc的xml文档
XmlDocument xmldoc = new XmlDocument();
//加入XML的声明段落:<?xmlversion="1.0" encoding="utf-8"?>
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(xmldecl);
//创建根节点
XmlElement xmlroot = xmldoc.CreateElement("CSUH");
xmldoc.AppendChild(xmlroot);
//创建根节点的子节点
XmlElement eParameter = xmldoc.CreateElement("M");
eParameter.SetAttribute("下限", "0.50");
eParameter.SetAttribute("上限", "1.80");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("λ");
eParameter.SetAttribute("下限", "0.010");
eParameter.SetAttribute("上限", "0.250");
eParameter.SetAttribute("步长", "0.001");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("κ");
eParameter.SetAttribute("下限", "0.01");
eParameter.SetAttribute("上限", "0.10");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("ν");
eParameter.SetAttribute("下限", "0.01");
eParameter.SetAttribute("上限", "0.49");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("N");
eParameter.SetAttribute("下限", "0.20");
eParameter.SetAttribute("上限", "2.50");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("χ");
eParameter.SetAttribute("下限", "0.10");
eParameter.SetAttribute("上限", "1.00");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("m");
eParameter.SetAttribute("下限", "0.1");
eParameter.SetAttribute("上限", "5.0");
eParameter.SetAttribute("步长", "0.1");
xmlroot.AppendChild(eParameter); eParameter = xmldoc.CreateElement("Z");
eParameter.SetAttribute("下限", "0.10");
eParameter.SetAttribute("上限", "1.25");
eParameter.SetAttribute("步长", "0.01");
xmlroot.AppendChild(eParameter); ////设置属性以及文本节点
////方法一 //person1.SetAttribute("name", "张三");
////方法二:这种方法可以用来获取和设置属性值,但前提是该属性要存在
//person2.Attributes["name"].Value = "张三三"; //保存创建好的XML文档
xmldoc.Save(@"D:\CSUH.xml");
MessageBox.Show("xml输出完成!", "提醒");
} private void gridControl1_Click(object sender, EventArgs e)
{
//if (e.Info.IsRowIndicator && e.RowHandle >= 0)
//{
// e.Info.DisplayText = (e.RowHandle + 1).ToString();
//}
}
//显示行号
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= )
{
e.Info.DisplayText = (e.RowHandle + ).ToString();
}
}
//单元的值变化的时候引起的事件
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
string s = string.Format("第{0}行,第{1}列,值改变为:{2:F5}。", e.RowHandle.ToString(), e.Column.VisibleIndex.ToString(), (e.Value).ToString());
MessageBox.Show(s); }
//得到GridControl单元格的值: string d =gridView1.GetRowCellDisplayText(i,col); i代表行号,col代表标题 } }

代码设定我需要的gridcontrol格式

效果如下:

c# 数据表DataTable给devexpress的gridControl提供数据源



https://jingyan.baidu.com/article/25648fc1d46dfb9191fd00c0.htmlc# 数据表DataTable给devexpress的gridControl提供数据源

c# 数据表DataTable给devexpress的gridControl提供数据源

             //绑定DataTable
gridControl1.DataSource = dt;
gridView1.OptionsCustomization.AllowColumnMoving = false;//如何让各列头禁止移动
gridView1.OptionsCustomization.AllowFilter = false;//不允许过滤
gridView1.OptionsCustomization.AllowSort = false;//不允许排序
gridView1.OptionsCustomization.AllowGroup = false;//不允许分组
gridView1.OptionsView.ShowGroupPanel = false;//不显示组面板
gridView1.OptionsSelection.MultiSelect = true;//可以多选

gridcontrol-gridview各种设置


添加行号:

c# 数据表DataTable给devexpress的gridControl提供数据源

参考:http://www.cnblogs.com/liulunet/archive/2011/08/08/2131227.html

取第三行第三列单元格的值:
gridView2.GetDataRow(2)[2]
         private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle >= )
{
e.Info.DisplayText = (e.RowHandle + ).ToString();
}
}

gridcontrol-GridView显示行号

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("字段名");

c# 数据表DataTable给devexpress的gridControl提供数据源

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");

创建列

c# 数据表DataTable给devexpress的gridControl提供数据源
//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);
c# 数据表DataTable给devexpress的gridControl提供数据源

创建行

c# 数据表DataTable给devexpress的gridControl提供数据源
//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);
c# 数据表DataTable给devexpress的gridControl提供数据源

赋值和取值

c# 数据表DataTable给devexpress的gridControl提供数据源
//新建行的赋值
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();
c# 数据表DataTable给devexpress的gridControl提供数据源
 
 
 
 
 
 
 
 
上一篇:Material Design之CoordinatorLayout+AppBarLayout实现上滑隐藏ToolBar


下一篇:jQuery学习笔记(jquery.ui插件)