作者: littleboy
2023-10-14 16:09:22
DataGradview
获得选择表格
要获取 DataGridView 中选定的单元格,可以使用 SelectedCells 属性。以下是一个示例:
1 2 3
| List<object> selectedValues = new List<object>(); foreach (DataGridViewCell cell in dataGridView1.SelectedCells) {selectedValues.Add(cell.Value);}
|
获得列数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| private void DataTablRow() { #region 方法1:基于行的循环 List<object> columnDataList = new List<object>(); int columnIndex = 1;
foreach (DataGridViewRow row in dataGridView1.Rows) { if (!row.IsNewRow) { object cellValue = row.Cells[columnIndex].Value; columnDataList.Add(cellValue); } } #endregion
#region 方法2:LINQ var cellValueCollection = dataGridView1.Rows .Cast<DataGridViewRow>() .Where(row => !row.IsNewRow) .Select(row => row.Cells["Age"].Value) .ToList();
object[] columnDataArray = cellValueCollection.ToArray();
int?[] ageArray = dataGridView1.Rows .Cast<DataGridViewRow>() .Where(row => !row.IsNewRow) .Select(row => { if (row.Cells["Age"].Value != null && int.TryParse(row.Cells["Age"].Value.ToString(), out int val)) return (int?)val; return (int?)null; }) .ToArray(); #endregion
#region 方法3:DataTable if (dataGridView1.DataSource is DataTable dt) { object[] columnDataList2 = dt.AsEnumerable().Select(row => row["ERP物料编号"]).ToArray(); DataRow[] rows = dt.Select(); } #endregion
string 机壳编码 = (string)Microsoft.VisualBasic.Interaction.Switch(columnDataList.ToArray()); }equationMgr.Equation[i].Split('=')[1].Trim().All(char.IsNumber)
|