/// <summary> /// 新增一行并且将其状态设置为选中 /// 参数意义参见 int AddRow(...) /// </summary> /// <param name="dgv"></param> /// <param name="item"></param> public static int AddRowAndSelect <T>(this DataGridView dgv, T item) where T : class { int rowIdx = dgv.AddRow(item); dgv.ClearSelection(); dgv.SelectRowAt(rowIdx); return(rowIdx); }
/// <summary> /// 向DataGridView中新增多行 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dgv"></param> /// <param name="list"></param> public static void AddRows <T>(this DataGridView dgv, IEnumerable <T> list) where T : class { Debug.Assert(dgv != null); dgv.Clear(); if (list != null) { #if DATA_GRID_VIEW_USE_DATASOURCE dgv.DataSource = list; #else foreach (var item in list) { dgv.AddRow(item); } #endif } dgv.ClearSelection(); }