嗨,谢谢你。我在实施我在此处找到的建议时遇到问题:Convert contents of DataGridView to List in C#
我从Master页面抓取一个GridView,我需要在表中添加一行然后重新绑定它。我通过Master页面上的公共属性访问GridView,该页面正在返回数据。基本上我已经这样做了:
var gsr = master.GridSearchResults;
gsr (gsr != null)
{
var so = new List<MyProperties>();
so = gsr .Rows.OfType<DataGridViewRow>().Select(r => r.Cells.OfType<DataGridViewCell>().Select(c => c.Value).ToArray()).ToList<MyProperties>();
so.Add(new MyProperties()
{
Id = id,
Date = date,
Building = buildingName,
Street = streetName
}
gsr.DataSource = so;
gsr.DataBind();
}
但是我收到一个错误,它无法转换实例参数类型System.Collections.Generic.IEnumerable&lt;&#39; object []&#39;&gt;到System.Collections.Generic.IEnumerable&lt;&#39; MyProperties&#39;&gt;。我认为问题是对数组的调用,但如果我删除它,那么我只是得到相同错误的变体。
答案 0 :(得分:0)
原来我的问题是我不应该首先获得gridview,而是获取Gridview的会话源。所以我解决了这个问题:
var master = (Tab)Master;
master.GridSearchResults.DataSource = null;
var sessions = new Sessions();
sessions.SlideOutSource.Add(new MyProperties {
Id = Id,
StartDate = hidStartDate.Value,
Installation = txtInstall.Text,
Command = txtCommand.Text });
master.GridSearchResults.DataSource = sessions.SlideOutSource;
master.GridSearchResults.DataBind();