日期:2014-05-18  浏览次数:20993 次

((DataTable)dgv_Account.DataSource).Rows.Add();报错了
C# code

DataSet ds_fy = Maticsoft.DBUtility.DbHelperSQL.Query(sql_fv);
                    if (ds_fy.Tables[0].Rows.Count > 0)
                    { 
                        //遍历数据集
                        for (int k = 0; k < ds_fy.Tables[0].Rows.Count; k++)
                        {
                            string fyName = ds_fy.Tables[0].Rows[k][0].ToString();
                            string fyprice = ds_fy.Tables[0].Rows[k][1].ToString();
                            string fypricefloat = ds_fy.Tables[0].Rows[k][2].ToString();

                            ((DataTable)dgv_Account.DataSource).Rows.Add();//此处报错 报错看图片
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[2].Value = txt_wtdh.Text;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[3].Value = txt_bl_no.Text;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[4].Value = cbb_WeiDan.Text;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[5].Value = ctnno;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[6].Value = ctntype;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[7].Value = ctnsize;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[8].Value = fyName;
                            dgv_Account.Rows[dgv_Account.Rows.Count - 1].Cells[9].Value = fyprice;

                        }
                    }






------解决方案--------------------
你要添加行的地方这样吧
C# code

// 绑定的地方还是 ToList
dgv_Account.DataSource = ysxx.ToList();

// 添加行的地方
var list = new List<你的类型>();
list.AddRange((List<你的类型>)dgv_Account.DataSource);
list.Add(你要添加的新行实例);
dgv_Account.DataSource = list;

------解决方案--------------------
C# code

var list = new List<Tbl_Fee_Account>();
list.AddRange(dgv_Account.DataSource as List<Tbl_Fee_Account>);
list.Add(new Tbl_Fee_Account());
dgv_Account.DataSource = list;