Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there ,
I want to insert value which I select from datagridview ,I have taken check box in datagridview.When i am selecting the value i am getting string ,but i want its value in int.my code is
C#
foreach (DataGridViewRow row in dtstudent.Rows)
               {
                   getbatchid();
                   DataGridViewCheckBoxCell cell = row.Cells["PRESENT"] as DataGridViewCheckBoxCell;
                   ClsConnection.Conn.Close();
                   if (cell.Value != null)
                   {
                       //MessageBox.Show(row.Cells[0].Value.ToString());
                       string s = row.Cells[0].Value.ToString();

                       string str11 = "";
                       string bat_date = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                       str11 = "insert into tbbatch_att(batch_id,student_id,staff_id,attendance_date)values(" + batchid + "," + s + "," + staffid + ",'" + bat_date + "')";

                       SqlCommand cmd11 = new SqlCommand(str11, ClsConnection.Conn);
                       ClsConnection.Conn.Open();
                       cmd11.ExecuteNonQuery();
                       ClsConnection.Conn.Close();

                   }


can any1 help plzzzzz
Posted
Updated 26-Jan-12 19:05pm
v2
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 1:26am    
Not a question. What's the problem?
--SA
Scubapro 27-Jan-12 1:58am    
I hope your checkbox column isn't the first column of your datagridview, or else your 'string s = row.Cells[0].Value.ToString();' will be either "true" or "false".

1 solution

Hi,

Parse the string to int.
C#
int numVal = Convert.ToInt32(yourstring);

or
C#
int num = int.Parse(text);

Hope this helps.
 
Share this answer
 
v2
Comments
SantoshRohinSantosh 27-Jan-12 1:21am    
If i do that it is giving me error.
Input string is not in correct format.
manognya kota 27-Jan-12 1:35am    
did you try this way?


string s = row.Cells[0].Value.ToString();
int numVal = Convert.ToInt32(s);
SantoshRohinSantosh 27-Jan-12 1:47am    
ya i did but not working
manognya kota 27-Jan-12 1:50am    
On debug ,check the format of the string that you are getting in 's' variable.If there are any spaces or any others charecters or a null value.If there are spaces, trail off the spaces and convert/parse to int.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900