Inline Editing and Updating in GridView with check box

Editing and updating in gridview is very simple. But how can you edit when you use checkbox.

GirdView contain CheckBoxField that display checkbox in each item in GridView control. This column
Fields is commonly use to display field with Boolean value.

  • If you use SQL SERVER and the Datatype for this field is bit then no problem for binding CheckBoxField data in GridView.
  • But you Datatype char(1) or anything and you insert like "T" or "F" then CheckBoxField is not bind.
  • Hear we can see how to solve this type of problem.

Step 1: Your .aspx page like this.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Untitled Page</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div align="center">  
  11.         <table align="center">  
  12.             <tr>  
  13.                 <td>  
  14.                     <asp:GridView ID="gvuserinfo" runat="server" AllowPaging="True" AutoGenerateColumns="False"  
  15.                         AutoGenerateEditButton="True"  
  16.                         OnPageIndexChanging="gvuserinfo_PageIndexChanging"  
  17.                         onrowcancelingedit="gvuserinfo_RowCancelingEdit"  
  18.                         onrowediting="gvuserinfo_RowEditing"  
  19.                         onrowupdating="gvuserinfo_RowUpdating">  
  20.                         <Columns>  
  21.                             <asp:BoundField DataField="userid" HeaderText="Userid" ReadOnly="True" >                                 
  22.                             </asp:BoundField>  
  23.                             <asp:BoundField HeaderText="User Name" DataField="username" />  
  24.                             <asp:CheckBoxField DataField="allowplan" Text="" HeaderText="Allowed For Plan" />                            
  25.                         </Columns>  
  26.                     </asp:GridView>  
  27.                 </td>                 
  28.             </tr>  
  29.             <tr>  
  30.                 <td>  
  31.                     <asp:Label ID="lblmessage" runat="server" ForeColor="Red"></asp:Label>  
  32.                 </td>  
  33.             </tr>  
  34.         </table>  
  35.     </div>  
  36.     </form>  
  37. </body>  
  38. </html> 

Step 2: your .cs file like this

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.HtmlControls;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;   
  10.  
  11. #region :: Add using ::  
  12. using System.Collections;  
  13. #endregion  
  14.   
  15. public partial class _Default : System.Web.UI.Page  
  16. {  
  17.     #region :: Variable Declaration::  
  18.       // DataAccess Class.  
  19.     clsGeneral objGeneral = new clsGeneral();  
  20.     #endregion  
  21.     protected void Page_Load(object sender, EventArgs e)  
  22.     {  
  23.         if (!IsPostBack)  
  24.         {  
  25.             //Bind The Gridview.  
  26.             BindGridView();  
  27.         }  
  28.     }  
  29.  
  30.     #region :: Bind GridView ::  
  31.     private void BindGridView()  
  32.     {  
  33.         string connection = objGeneral.Getconnection();  
  34.         string sql = "SELECT userid, username, allowforfreeplan FROM UserInfo ORDER BY username";  
  35.         DataTable dtUserInfo = new DataTable();  
  36.         dtUserInfo = objGeneral.SelectRows(dtUserInfo, connection, sql);  
  37.   
  38.         /*Create new column for database. 
  39.          * this column is user for bind  ChecBoxField. 
  40.         */  
  41.         DataColumn column = new DataColumn();  
  42.         column.DataType = System.Type.GetType("System.Boolean");  
  43.         column.ColumnName = "allowplan";  
  44.   
  45.         //Add column in table.  
  46.   
  47.         dtUserInfo.Columns.Add(column);  
  48.   
  49.         if ((dtUserInfo != null) && (dtUserInfo.Rows.Count > 0))  
  50.         {  
  51.                         for (int i = 0; i < dtUserInfo.Rows.Count; i++)  
  52.             {  
  53.                 string type = dtUserInfo.Rows[i]["allowforfreeplan"].ToString().ToUpper();  
  54.                 if (type == "T")  
  55.                 {  
  56.                     dtUserInfo.Rows[i]["allowplan"] = true;  
  57.                 }  
  58.                 else  
  59.                 {  
  60.                     dtUserInfo.Rows[i]["allowplan"] = false;  
  61.                 }  
  62.             }  
  63.             gvuserinfo.DataSource = dtUserInfo;  
  64.             gvuserinfo.DataBind();  
  65.         }  
  66.     }  
  67.     #endregion  
  68.     protected void gvuserinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  69.     {  
  70.         gvuserinfo.PageIndex = e.NewPageIndex;  
  71.         BindGridView();  
  72.     }  
  73.     protected void gvuserinfo_RowEditing(object sender, GridViewEditEventArgs e)  
  74.     {  
  75.         gvuserinfo.EditIndex = e.NewEditIndex;  
  76.         BindGridView();  
  77.     }  
  78.     protected void gvuserinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  79.     {  
  80.         gvuserinfo.EditIndex = -1;  
  81.         BindGridView();  
  82.     }  
  83.     protected void gvuserinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  84.     {  
  85.         int userid = Convert.ToInt32(gvuserinfo.Rows[e.RowIndex].Cells[1].Text);  
  86.         TextBox txtname = (TextBox)gvuserinfo.Rows[e.RowIndex].Cells[2].Controls[0];  
  87.         CheckBox cb = (CheckBox)gvuserinfo.Rows[e.RowIndex].Cells[3].Controls[0];  
  88.         string allowforfree = string.Empty;  
  89.   
  90.         if (cb.Checked == true)  
  91.         {  
  92.             allowforfree = "T";  
  93.         }  
  94.         else  
  95.         {  
  96.             allowforfree = "F";  
  97.         }  
  98.         string sql = "UPDATE [dbo].[UserInfo]  SET [username] = '" + txtname.Text + "',[allowforfreeplan] = '" + allowforfree + "'";  
  99.         sql += " WHERE  [dbo].[UserInfo].[userid] = " + userid;  
  100.         string connection = objGeneral.Getconnection();  
  101.         int i = objGeneral.InsertUpdateData(connection, sql);  
  102.         if (i == 1)  
  103.         {  
  104.            // lblmessage.Text = "Record Updated Succesfully ...";  
  105.         }  
  106.         gvuserinfo.EditIndex = -1;  
  107.         BindGridView();  
  108.     }  
  109.   

Step 3: Your DataAccess Class like.

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.HtmlControls;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Data.SqlClient;  
  11.   
  12. /// <summary>  
  13. /// Summary description for clsGeneral  
  14. /// </summary>  
  15. public class clsGeneral  
  16. {  
  17.     protected string test = ConfigurationManager.ConnectionStrings["SQLCONNECTION"].ToString();  
  18.       public clsGeneral()  
  19.       {  
  20.             //  
  21.             // TODO: Add constructor logic here  
  22.             //  
  23.       }  
  24.    
  25.     public string Getconnection()  
  26.     {  
  27.         return test;  
  28.     }  
  29.   
  30.     public  DataTable SelectRows(DataTable datatable,string connectionString, string queryString)  
  31.     {  
  32.         using (SqlConnection connection = new SqlConnection(connectionString))  
  33.         {  
  34.             SqlDataAdapter adapter = new SqlDataAdapter();  
  35.             adapter.SelectCommand = new SqlCommand(queryString, connection);  
  36.             adapter.Fill(datatable);  
  37.             return datatable;  
  38.         }  
  39.     }  
  40.   
  41.     public int InsertUpdateData(string connectionString,string sql)  
  42.     {  
  43.         int i;  
  44.         using (SqlConnection connection = new SqlConnection(connectionString))  
  45.         {  
  46.             connection.Open();  
  47.             SqlCommand com = new SqlCommand(sql, connection);  
  48.             i = com.ExecuteNonQuery();  
  49.             return i;  
  50.         }  
  51.   
  52.     }  


You Can read my other article on

http://www.c-sharpcorner.com

1) Gridview paging and multiple rows delete using checkbox.
2) How to create 3 tire application using LINQ
3) Binding Gridview using LINQ
4) How to create RSS (Really Simple Syndication)?
5) Gridview paging and multiple row delete using checkbox

Up Next
    Ebook Download
    View all
    Learn
    View all