关于table.LoadDataRow修改datable某一行的值

来源:互联网 发布:剑三雪河炮太捏脸数据 编辑:程序博客网 时间:2024/06/05 19:55

对于table.LoadDataRow (obj,null)的解释是:

当table中没有主键时就插入该新行,有主键时就更新对应行

DataTable table = new DataTable();            DataColumn col1 = new DataColumn("id", typeof(string));            DataColumn col2 = new DataColumn("name", typeof(string));            DataColumn col3 = new DataColumn("age", typeof(int));            table.Columns.Add(col1);            table.Columns.Add(col2);            table.Columns.Add(col3);            //给datatable设置主键            DataColumn[] key = new DataColumn[1];            key[0] = col1;            table.PrimaryKey = key;            //table.PrimaryKey = new DataColumn[] { table.Columns["id"] };            DataRow row = table.NewRow();            row[col1] = "G001";            row[col2] = "yanxiangliang";            row[col3] = "31";            table.Rows.Add(row);            row = table.NewRow();            row[col1] = "G002";            row[col2] = "zhangzhiguo";            row[col3] = "32";            table.Rows.Add(row);            row = table.NewRow();            row[col1] = "G003";            row[col2] = "ls";            row[col3] = "30";            table.Rows.Add(row);            Object[] find = new Object[3];            find[0] = "G003";            find[1] = "teswt";            find[2] = 45;            table.BeginLoadData();            table.LoadDataRow(find, LoadOption.OverwriteChanges);            table.EndLoadData();

可看到 更新了goo3那一行的数据,若改为goo4则会为新增行

其他应用:

删除datarow

            table.Rows.Remove(row4);删除航实例
            table.Rows.RemoveAt(3);//删除指定行


//对datatable排序
            DataView dv = table.DefaultView;
            dv.Sort = "age DESC";
            DataTable dt2 = dv.ToTable();

0 0
原创粉丝点击