.Net添加DataRow

来源:百度知道 编辑:UC知道 时间:2024/06/03 12:54:06
刚刚学习DataSet,遇到一个问题,代码如下
//下面是添加DataSet、DataTable和DataColum对象
private System.Data.DataSet dsBagCheck;

private void Form1_Load(object sender, System.EventArgs e)
{
dsBagCheck = new DataSet();

DataTable dtMyBags = new DataTable("MyBags");
dtMyBags.Columns.Add("BagID",System.Type.GetType("System.Int32"));
dtMyBags.Columns.Add("BagName",System.Type.GetType("System.String"));
dtMyBags.Columns.Add("BagColor",System.Type.GetType("System.String"));

dsBagCheck.Tables.Add(dtMyBags);
}
//下面功能是当单击Add按钮时,向MyBags表中添加行
private void buttonAdd_Click(object sender, EventArgs e)
{
int i;
DataRow drNewBag;
DataTableCollection tablesCol = dsBagCheck.Tables; //出错
DataTable dtMyBags =

DataTableCollection tablesCol = dsBagCheck.Tables;
DataTable dtMyBags = tablesCol["MyBags"];

上面2句合并成一句

DataTable dtMyBags = dsBgCheck.Tables["MyBags"];

不过,你原来的代码,也没问题啊,怪了

运行没有错误啊

拷你的代码运行了,确实没有错误