Forum guidelines
AuthorQuestion
how to validate in datagridview using C#
Posted By Naeem Khan on 21 Nov 2009
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
Naeem Khan (Software Developer)
AuthorReply
Re: how to validate in datagridview using C#
Posted By Rujuta on 21 Nov 2009  
Hi...
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

Re: how to validate in datagridview using C#
Posted By Kirtan Patel on 21 Nov 2009  
Friend,

could not understood you. can you explain a bit clear way  :)

MY BLOG ( www.codestructs.com ) | MY SITE

Kirtan
 Patel   BCA   MCA (SEMESTER-IV)  MINDCRACKER MVP WEB HACKER



Re: how to validate in datagridview using C#
Posted By Nilanka Dharmadasa on 21 Nov 2009  
Hi Naim,


        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.


Nilanka Dharmadasa

Re: how to validate in datagridview using C#
Posted By Naeem Khan on 22 Nov 2009  
hi, well my problem is that i want to make validation in data grid view . at least one check box should be selected . if its not then give some error.... hope you understand what i want. please reply me fast.. thanks in Advance
Naeem Khan (Software Developer)
Re: how to validate in datagridview using C#
Posted By Kirtan Patel on 22 Nov 2009  
Friend,

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 !!");

  }

 

}



MY BLOG ( www.codestructs.com ) | MY SITE

Kirtan
 Patel   BCA   MCA (SEMESTER-IV)  MINDCRACKER MVP WEB HACKER



Nilanka Dharmadasa
  • 0
  • 0
accepted
Re: how to validate in datagridview using C#
Posted By Nilanka Dharmadasa on 23 Nov 2009   Accepted Answer
Hi Naim,

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)
        MessageBox.Show("Please select at least one record !!");

        }


Let me know, if you still have the problem.

If this helps, please mark my answer as accepted.

Nilanka Dharmadasa

Re: how to validate in datagridview using C#
Posted By Sam Hobbs on 23 Nov 2009  
That sounds like radio buttons. Can you use radio buttons instead? If all the data from all the checkboxes that must have only one checked is stored in just one field in the database, radio buttons will be easier. In order to do that, you need to create a UserControl for showing the field and have the radio buttons in the UserControl. Do you want to do that?
Thinking is a feeling; pleasant for some and unpleasant for others.
Re: how to validate in datagridview using C#
Posted By Pravakar Jaiswal on 23 Nov 2009  

As far as i understood your problem. it can be done by either using clinet side or server side.
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>
....
....
....
</columns>
</asp:GridView>
 In click event of Button:
bool temp=false;

for
(int i = 0; i < gdReportResult.Rows.Count; 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;
});
}

Pravakar Jaiswal

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.