How to validate in datagridview using C#
hello friends,
well i have big problem i have datagridview which is bind from database with check boxes if i insert i selected check boxes value in database then second time it should not insert same records . do u have any idea how can i get validation of it. and atlest should be one checked before insert a record. if not then give some Exception to user hope u understand wht i want please help me guys
thanks in advance


Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 22, 2009, 8:03 PM EST
Sorry for the delay. Didnt see your reply.
Try this.
private void button1_Click(object sender, EventArgs e)
{
bool selected = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool cheked = ((bool)(row.Cells["CheckBoxColumnName"].EditedFormattedValue));
if (cheked)
selected = true;
}
if(!selected)
}
Let me know, if you still have the problem.
If this helps, please mark my answer as accepted.
Pravakar Jaiswal6 reputationPosted Nov 23, 2009, 6:57 PM EST
For server side use this.
<asp:GridView id="gdReportResult" runat="server" CssClass="blue_tbl" OnPageIndexChanging="gdReportResult_OnPageIndexChanged" OnSelectedIndexChanged="gdReportResult_selectedIndexchanged" AutoGenerateColumns="false" AllowPaging="false" Width="100%" SelectedIndex="0" AllowSorting="True"
OnSorting="gdReportResult_Sorting" OnRowDataBound="gdReportResult_RowDataBound" OnRowCommand="gdReportResult_RowCommand" cellpadding="5">
<Columns>
<asp:TemplateField HeaderText="" HeaderStyle-ForeColor="white">
<ItemTemplate>
<asp:CheckBox ID="ChkInv" runat="server" CssClass="checkBoxBatchInv">asp:CheckBox>
ItemTemplate>
....
....
....
for (int i = 0; i CheckBox ch = (CheckBox)gdReportResult.Rows[i].Cells[0].FindControl("ChkInv"); if (ch.Checked) { temp=true; break; } } if(temp) { // write your insert code. } else { //display please select at least one check box. }Using jquery you can also do this
On client Click of Button
function CheckGatagrid(){
var t="0";
jQuery.each($(".checkBoxBatchInv :checkbox"), function(index,id) {
//here checkBoxBatchInv is the cssClass of check box column
if(document.getElementById(id).checked=="checked"){
t="1";
break;
}
if(t=="0"){
alert('Please select at least one check box in grid');
return false;
}
else
return true;
});
}
Rujuta133 reputationPosted Nov 21, 2009, 2:00 AM EST
I did not understand your problem fully but I'll help the way I can. If you want the user to select a checkbox and then insert a value into the gridview and want to have a constraint that no one adds a similar record again then keep the autopostback of checkbox as well gridview true so that everytime user click the page is refreshed and they me see all the added record also do back end coding to search thru the gridview and see that no similar records are there.
You'll get a better answer if you explain in a better way with some example
Kirtan PatelRank #292 · 8.6K reputationPosted Nov 21, 2009, 2:04 AM EST
could not understood you. can you explain a bit clear way :)
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 21, 2009, 2:11 AM EST
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool cheked = ((bool)(row.Cells["CheckBoxColumnName"].EditedFormattedValue));
if (cheked)
continue;
else
{
//Write your code to insert the row.
}
}
}
If this is not what you need, let me know pls. If you like this answer, please mark the chekbox.
Naeem KhanRank #2,072 · 698 reputationPosted Nov 22, 2009, 6:13 PM EST
Kirtan PatelRank #292 · 8.6K reputationPosted Nov 22, 2009, 6:41 PM EST
here is Code how you can validate
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
int CurrentColumn = e.ColumnIndex;
if (Convert.ToBoolean(dataGridView1.Rows[rowIndex].Cells[0].Value) != true || Convert.ToBoolean(dataGridView1.Rows[rowIndex].Cells[1].Value) != true)
{
MessageBox.Show("you Must Choose One Chioice !!");
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2009, 8:09 PM EST