日期:2014-05-20  浏览次数:20649 次

linq to sql 操作datagirdview
我又来提问啦
以前发过帖子 解决了问题
看这里http://topic.csdn.net/u/20120602/16/46c3649b-0760-4d8b-94d8-de044279c846.html

但是现在又出现问题了
如果查询条件变化了,不是原先的类的字段名字了,该怎样在datagridview中新增一行呢?
因为之前都是 绑定的整个的表 现在只是单独取出了这几个单独的字段 就不行了呢
C# code

 StartLinqDataContext start = new StartLinqDataContext();
            
        private void button1_Click(object sender, EventArgs e)
        {
            var chaxun = from c in start.FeeMain select new { c.remark4,c.indatatime,c.wtdw,c.vessel,c.voyage,c.blno,c.accounts,c.pay,c.profit };
            if (cmb_wtdw.Text != "")
                chaxun = chaxun.Where(c => c.wtdw == cmb_wtdw.Text);
            chaxun = chaxun.Where(c => Convert.ToDateTime(c.indatatime) >=Convert.ToDateTime( dtp1.Value.ToString("yyyy/MM/dd 00:00:00")) 
                &&Convert.ToDateTime( c.indatatime) <=Convert.ToDateTime( dtp2.Value.ToString("yyyy/MM/dd 23:59:59")));
            
            dataGridView1.AutoGenerateColumns = false;
            
            dataGridView1.DataSource = chaxun.ToList();

            var list = new List<FeeMain>();
            list.AddRange(dataGridView1.DataSource as List<FeeMain>);//报错 值不能为空
            list.Add(new FeeMain());
            dataGridView1.DataSource = list;
        }



------解决方案--------------------
var chaxun = from c in start.FeeMain select new { c.remark4,c.indatatime,c.wtdw,c.vessel,c.voyage,c.blno,c.accounts,c.pay,c.profit };
=========
 var chaxun = from c in start.FeeMain select new TempClass{ remark4 =c.remark4 //后边自己加
};
var list = new List<TempClass>();
list.AddRange(dataGridView1.DataSource as List<TempClass>);//报错 值不能为空
list.Add(new TempClass());
dataGridView1.DataSource = list;


public class TempClass
{
public string remark4 {get;set;} 
//后边自己加
}