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 DharmadasaPosted Nov 23, 2009, 1:03 AM
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 JaiswalPosted Nov 23, 2009, 11:57 PM
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>
....
....
....
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;
});
}
Sam HobbsPosted Nov 23, 2009, 1:09 AM
Kirtan PatelPosted Nov 22, 2009, 11:41 PM
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 !!");
}
}
Naeem KhanPosted Nov 22, 2009, 11:13 PM
Nilanka DharmadasaPosted Nov 21, 2009, 7:11 AM
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.
Kirtan PatelPosted Nov 21, 2009, 7:04 AM
could not understood you. can you explain a bit clear way :)
RujutaPosted Nov 21, 2009, 7:00 AM
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