private T GetSelectedItem <T>() where T : class { int i = Gridview.GetSelectedRows().FirstOrDefault(); T obj = Gridview.GetRow(i) as T; return(obj); }
public void UpdateRow <T>(T entry) { var row = Gridview.GetSelectedRows().FirstOrDefault(); foreach (GridColumn col in Gridview.Columns) { var props = col.FieldName.Split('.'); { var propInfo = entry.GetType().GetProperty(props[0]); if (propInfo != null) { var val = propInfo.GetValue(entry, null); if (val != null) { for (var i = 1; i < props.Length; i++) { propInfo = val.GetType().GetProperty(props[i]); val = propInfo.GetValue(val, null); } Gridview.SetRowCellValue(row, col, val); } } } } }
private void DatagridviewMain_ProcessGridKey(object sender, KeyEventArgs e) { if (e.KeyData == Keys.Delete) { DialogResult result = FormUtility.MsgDelete(); if (result == DialogResult.Yes) { int i = Gridview.GetSelectedRows().FirstOrDefault(); object obj = Gridview.GetRow(i) as object; if (obj == null) { return; } DeleteRow <object>(obj); Gridview.DeleteRow(i); } } e.Handled = true; }