17、數據驗證
Validate Data in the Windows Forms DataGridVIEw Control
Samples:
private void dataGridVIEw1_CellValidating(object sender,
DataGridVIEwCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
if (dataGridVIEw1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (e.FormattedValue.ToString() == String.Empty)
{
dataGridVIEw1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}
}
18、數據提交到dataset中
DataSet ds = new DataSet("MyDataSet");
ds.Tables[biaom.Trim()].Rows.Clear();
try
{
for (int i = 0; i < dataGridVIEw1.Rows.Count - 1; i++)
{
DataTable dt = ds.Tables[biaom.Trim()];
DataRow myrow = ds.Tables[biaom.Trim()].NewRow();
for (int j = 0; j < dataGridVIEw1.Columns.Count; j++)
{
myrow[j] = Convert.ToString(dataGridVIEw1.Rows[i].Cells[j].Value);
}
ds.Tables[biaom.Trim()].Rows.Add(myrow);
}
}
catch (Exception)
{
MessageBox.Show("輸入類型錯誤!");
return;
}