码迷,mamicode.com
首页 > 其他好文 > 详细

C# DataTable的用法

时间:2014-10-10 13:11:14      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:datagridview   os   ar   for   sp   on   ad   new   bs   

建表
DataTable dtP = new DataTable("Man");
加列
DataColumn dcP;
dcP =
new DataColumn("Name",Type.GetType("System.String"));
dcP.ReadOnly =
false;
dcP.AllowDBNull = false;
dcP.Unique =
true;
dtP.Columns.Add(dcP);
dcP = new DataColumn("Sexy",
Type.GetType("System.Int32"));
dcP.ReadOnly = false;
dcP.AllowDBNull =
false;
dcP.Unique = false;
dtP.Columns.Add(dcP);
dcP = new
DataColumn("Briday", Type.GetType("System.DateTime"));
dcP.ReadOnly =
false;
dcP.AllowDBNull = false;
dcP.Unique =
false;
dtP.Columns.Add(dcP);
加主键
DataColumn[] PK = new
DataColumn[1];
PK[0] = dtP.Columns["Name"];
dtP.PrimaryKey =
PK;
新建行
DataRow newRow;
newRow = dtP.NewRow();
newRow["Name"] =
textBox1.Text;
newRow["Sexy"] = checkBox1.Checked ? 1 :
0;
newRow["Briday"] =
Convert.ToDateTime(dateTimePicker2.Text);
dtP.Rows.Add(newRow);
dtP.AcceptChanges();
邦定DateGridView
dataGridView1.DataSource
=
dtP;
删除行
dtP.Rows[dataGridView1.CurrentRow.Index].Delete();
dtP.AcceptChanges();
更新行
int
dvIndex = dataGridView1.CurrentRow.Index;
string filterStr = "Name=‘" +
dataGridView1.Rows[dvIndex].Cells[0].Value.ToString() + "‘";
string tmpName =
null;
string tmpSexy = null;
string tmpBriday = null;
DataRow[] selRows
= dtP.Select(filterStr);
for (int i = 0; i < selRows.Length;
i++)
{
    DataRow temp = selRows[i];
   
tmpName += temp["Name"] = textBox1.Text;
    tmpSexy +=
temp["Sexy"] = checkBox1.Checked ? 1 : 0;
    tmpBriday +=
temp["Briday"] = dateTimePicker2.Text;
    selRows[i] =
temp;
}

C# DataTable的用法

标签:datagridview   os   ar   for   sp   on   ad   new   bs   

原文地址:http://www.cnblogs.com/qimucheng/p/4015553.html

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