我需要在DataGridView的现有单元格或列中验证数据值类型是否为整数类型。
我能够选择数据网格中的单元格并对其进行计数,然后在MessageBox中显示选中了多少个单元格:
private void button1_Click(object sender, EventArgs e)
{
Int32 selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
if (dataGridView1.AreAllCellsSelected(true))
{
MessageBox.Show("All cells are selected", "Selected Cells");
}
else
{
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
for (int i = 0;
i < selectedCellCount; i++)
{
sb.Append("Row: ");
sb.Append(dataGridView1.SelectedCells[i].RowIndex
.ToString());
sb.Append(", Column: ");
sb.Append(dataGridView1.SelectedCells[i].ColumnIndex
.ToString());
sb.Append(Environment.NewLine);
}
sb.Append("Total: " + selectedCellCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Cells");
}
}
}
我希望现有的单元格值是否为整数类型都可以得到验证,并在MessageBox中显示结果。