You must Sign In to post a response.
  • Category: ASP.NET

    Checkbox inside gridview with update query in asp net

    checkbox inside gridview with update query in asp net
  • #756344
    Refer the below code

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#334399" BorderStyle="Solid" BorderWidth="1px"
    CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
    Font-Names="Arial" GridLines="Vertical" Width="40%">

    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="chkStatus" runat="server"
    AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
    Checked='<%# Convert.ToBoolean(Eval("Sucess")) %>'
    Text='<%# Eval("Approved").ToString().Equals("True") ? " Sucess " : " Failure " %>' />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:BoundField DataField="ID" HeaderText="ID" />
    <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>

    <HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />

    </asp:GridView>


    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    LoadData();
    }
    }

    private void LoadData()
    {
    string constr = [Connection String]
    string query = @"SELECT ID, Name,Status FROM [Table Name]";

    SqlDataAdapter da = new SqlDataAdapter(query, constr);
    DataTable table = new DataTable();
    da.Fill(table);

    GridView1.DataSource = table;
    GridView1.DataBind();
    }


    public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
    {
    CheckBox chkStatus = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chkStatus.NamingContainer;


    string cid = row.Cells[1].Text;
    bool status = chkStatus.Checked;


    string constr = [Connection String]
    string query = "UPDATE [TableName] SET Status = @Status WHERE ID = @ID";

    SqlConnection con = new SqlConnection(constr);
    SqlCommand com = new SqlCommand(query, con);


    com.Parameters.Add("@Status", SqlDbType.Bit).Value = status;
    com.Parameters.Add("@ID", SqlDbType.Int).Value = cid;


    con.Open();
    com.ExecuteNonQuery();
    con.Close();

    LoadData();
    }

  • #756347
    Hi,

    I believe you are looking for the code to insert the data into the database based on the checkbox selection.
    If that is the case then please refer:
    http://stackoverflow.com/questions/25316392/update-gridview-according-to-checkbox


    Regards,
    Asheej T K

  • #756353
    Hi,

    Question is not clear, please edit the question and re-post again.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments