I want only 3 of the columns in my table in Datagridview1 not to be empty. I want it to warn the screen when there is an empty field.
I tried to make a mandatory field to be filled in the datagridview1 table
When I add other data to the data I pull from sql in Datagridview, I want it to warn the screen to be filled if there should be no empty space.
As you can see in the photo below, only those places should not be empty, and when I press save, it should detect when there is an empty space and give a warning to the screen.
give a warning if there is free space
private void btn_Save_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Select * from Envanter where BilgNo,VarlikSahibi,IP", baglanti);
cmd.Parameters.AddWithValue("BilgNo", dataGridView1);
cmd.Parameters.AddWithValue("VarlikSahibi", dataGridView1);
cmd.Parameters.AddWithValue("IP", dataGridView1);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
commandBuilder = new SqlCommandBuilder(da);
da.Update(tablo);
MessageBox.Show("Saved");
Envanter();
return;
/*
if (dataGridView1.Rows.Cells[1].Value == null | Convert.ToString(row.Cells[1].Value) | string.IsNullOrEmpty)
{
DialogResult dr = MessageBox.Show("pls no empty", "Warn!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return;
*/
}
}
CodePudding user response:
First Iterate over the dataset rows and then iterate over the value of each cell if there are any empty values return the message box of your choice to the user. Please find the below attached code for your reference.
You can do the following :
foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i )
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())
{
// here is your input message box...
string promptValue = Prompt.ShowDialog("Message", "some other string you want!");
}
else
{
// Proceed with the things you want to do..
}
}
}
And use the below code to create an input popup:
public static class Prompt
{
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
TextBox textBox = new TextBox() { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70, DialogResult = DialogResult.OK };
confirmation.Click = (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
}
And you can call it using :
string promptValue = Prompt.ShowDialog("Message", "some other string you want!");
CodePudding user response:
If you are using plain C# model classes, you can rely on data annotations validation attributes.
If you are using DataTable as the model, you can validate the DataTable by setting column or row errors and check if the data table has errors.
You can also look over the rows or cells of DataGridView and set cell or row errors, with or without databinding.
Example - Validating Cells of DataGridView and show error on cells
private bool ValidateCell(DataGridViewCell cell)
{
//Validation rules for Column 1
if (cell.ColumnIndex == 1)
{
if (cell.Value == null ||
cell.Value == DBNull.Value ||
string.IsNullOrEmpty(cell.Value.ToString()))
{
cell.ErrorText = "Required";
return false;
}
else
{
cell.ErrorText = null;
return true;
}
}
//Other validations
return true;
}
private bool ValidateDataGridView(DataGridView dgv)
{
return dgv.Rows.Cast<DataGridViewRow>()
.Where(r => !r.IsNewRow)
.SelectMany(r => r.Cells.Cast<DataGridViewCell>())
.Select(c => ValidateCell(c)).ToList()
.All(x => x == true);
}
Then use above methods before saving the data:
private void SaveButton_Click(object sender, EventArgs e)
{
if (!ValidateDataGridView(dataGridView1))
{
MessageBox.Show("Please fix validation errors");
return;
}
//Save changes;
}
To show the errors on cells or rows of DataGridView, set ShowRowErrors and ShowCellErrors to true.
dataGridView1.DataSource = dt;
dataGridView1.ShowCellErrors = true;
Also, if you want to update the validations of the cells, after editing the cell:
private void DataGridView1_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
{
ValidateCell(dataGridView1[e.ColumnIndex, e.RowIndex]);
}