Get Selected Checkbox Value from Gridview in Asp.net C#

In this Blog as Work with in gridview Selected Checkbox ID Value can Get In C#.


ASPX Code:


  <div style="width: 104%; height: 243px; max-height: 243px; overflow: auto;">
   <asp:GridView ShowHeader="false" AutoGenerateColumns="false"                     ID="chklCategory" runat="server">
 <Columns>
                                                                                    <asp:TemplateField>
                                                                                        <ItemTemplate>
    <asp:CheckBox runat="server" AutoPostBack="true" ID="Checkbox_Category" Text='<%#bind("Name") %>' />
                                                                                              <asp:HiddenField runat="server" ID="hidCheck" Value='<%# bind("Id") %>' />
                                                                                       </ItemTemplate>
                                                                                        <ItemStyle HorizontalAlign="Left" />
                                </asp:TemplateField>
 </Columns>
 </asp:GridView>

C# Code:

The Gridview Selected Value can store Two ways. Store Database and Store data to String. 
In this Function can perform selected Value Can store the DataTable.

public DataTable DTCatIds()
        {
            //string str = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            foreach (GridViewRow row in chklCategory.Rows)
            {
                CheckBox Checkbox = (CheckBox)row.FindControl("Checkbox_Category");
                if (Checkbox.Checked)
                {
                    HiddenField hidCheck = (HiddenField)row.FindControl("hidCheck");
                    //str += hidCheck.Value + ",";
                    dt.Rows.Add(hidCheck.Value);
                    dt.NewRow();
                }
            }
            return dt;
            //return str;
        }

---------------------------------------------------------------
public String DTCatIds()
        {
            string str = "";
           
            foreach (GridViewRow row in chklCategory.Rows)
            {
                CheckBox Checkbox = (CheckBox)row.FindControl("Checkbox_Category");
                if (Checkbox.Checked)
                {
                    HiddenField hidCheck = (HiddenField)row.FindControl("hidCheck");
                    str += hidCheck.Value + ",";

                }
            }
            return str;
            
        }

Comments

Popular posts from this blog

Insecure cookie setting: missing Secure flag

Maximum Stored Procedure Function Trigger or View Nesting Level Exceeded (limit 32) in SQL Server

Display Line Chart Using Chart.js MVC C#