110,537
社区成员
发帖
与我相关
我的任务
分享
public class ConvertHelper<T,U> where T : new() where U:new()
{
/// <summary>
/// 利用反射和泛型
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> FillModel(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}
List<T> modelList = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T model = new T();
for (int i = 0; i < dr.Table.Columns.Count; i++)
{
PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
if (propertyInfo != null && dr[i] != DBNull.Value)
propertyInfo.SetValue(model, dr[i], null);
}
modelList.Add(model);
}
return modelList;
}
}
我传过来2个类型 用泛型类, 怎么让他返回2个类型的字段呢. 我datatable是关联2个表部分结果的字段,如何提取到 private void CreatePages()
{
ClearPages();
if (dataSource == null)
{
return;
}
radGridView1.DataSource = dataSource;
radGridView1.LoadElementTree();
Application.DoEvents();
if (isSizeChanged)
{
numberOfRows = radGridView1.TableElement.VisualRows.Count(c => c.RowInfo is GridViewDataRowInfo) - 1;
numberOfRows = numberOfRows != 0 ? numberOfRows : 1;
}
var list = radGridView1.MasterTemplate.CurrentView.Rows.Select(r => r.DataBoundItem).ToList();
pagesDictionary.Clear();
IList pageSource = null;
for (int i = 0; i < list.Count; i++)
{
int pageNo = i / numberOfRows + 1;
if (!pagesDictionary.ContainsKey(pageNo))
{
pageSource = Activator.CreateInstance(dataSource.GetType()) as IList;
pagesDictionary.Add(pageNo, pageSource);
CommandBarToggleButton pageButton = new CommandBarToggleButton();
stripElementPages.Items.Add(pageButton);
pageButton.ToggleStateChanging += new StateChangingEventHandler(pageButton_ToggleStateChanging);
pageButton.ToggleStateChanged += new StateChangedEventHandler(pageButton_ToggleStateChanged);
pageButton.Text = pageNo.ToString();
pageButton.Tag = pageNo;
pageButton.Image = null;
pageButton.DrawText = true;
}
pageSource = pagesDictionary[pageNo] as IList;
pageSource.Add(list[i]);
}
lblNumPages.Text = pagesDictionary.Count.ToString();
radCommandBar1.Visible = true;
}
//它这个页的数据源是ilist的 但是传入过来的是一个datatable,datatable是查询出来返回2个表的内容