Solved

Binding a check box in an Asp.net DataGrid

Posted on 2016-11-15
4
26 Views
Last Modified: 2016-11-20
I have created a DataGrid control (NOT GRIDVIEW). The grid has two columns. One for the persons name and one for a check box control.
I want to be able to set the value for the Check box, in a similar way that I do for the persons name. In other words using C#, by binding it to a data source. So how can I do that for the check box control? Also note that my check box control is in an ItemTemplate and TemplateColumn.

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700px" EnableViewState="False">
<Columns>

<asp:TemplateColumn HeaderText="cltCheckBox">
<ItemTemplate>
      <asp:CheckBox ID="Check" Checked=false runat="server">

      </asp:CheckBox>
</ItemTemplate>

<asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Name">
   <HeaderStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</asp:Content>



protected void Page_Load(object sender, EventArgs e)
{
  DataSet ds = new DataSet();
  DataTable dt = new DataTable();
  dt.Columns.Add("Name", Type.GetType("System.String"));

  dt.Rows.Add(new object[] {James Bond"});
  ds.Tables.Add(dt);
  MyDataGrid.DataSource = dt;
  MyDataGrid.DataBind();

}
0
Comment
Question by:brgdotnet
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 150 total points
ID: 41889115
Hi brgdotnet,

Here it is, Please try...

Aspx

<asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="false" runat="server">
            <Columns>

            <asp:TemplateColumn HeaderText="cltCheckBox">
                <ItemTemplate>
                      <asp:CheckBox ID="Check" Checked=false runat="server">

                      </asp:CheckBox>
                </ItemTemplate>
            </asp:TemplateColumn>

            <asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Name">              
            </asp:BoundColumn>

            </Columns>
        </asp:DataGrid>

Open in new window


Code behind


protected void Page_Load(object sender, EventArgs e)
    {
      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      dt.Columns.Add("Name", Type.GetType("System.String"));
      dt.Rows.Add(new object[] {"James Bond"});
      ds.Tables.Add(dt);
      MyDataGrid.DataSource = dt;
      MyDataGrid.DataBind();

    }

Open in new window



Output


EE
Hope it helps !!

Regards,
Pawan
0
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 350 total points
ID: 41889145
Hi,

You can bind the checkbox and other columns as follows.

  <asp:DataGrid ID="MyDataGrid" AutoGenerateColumns="false" runat="server">
                <Columns>
                    <asp:TemplateColumn HeaderText="cltCheckBox">
                        <ItemTemplate>
                            <asp:CheckBox ID="Check" runat="server" Checked='<%# Eval("IsChecked") %>'></asp:CheckBox>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Name"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>

Open in new window




protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();

                dt.Columns.Add("IsChecked", typeof(bool));
                dt.Columns.Add("Name", typeof(string));

                dt.Rows.Add(true, "Scott");
                dt.Rows.Add(false, "Tom");
                dt.Rows.Add(false, "John");
                dt.Rows.Add(true, "Wilson");

                ds.Tables.Add(dt);
                MyDataGrid.DataSource = dt;
                MyDataGrid.DataBind();
            }
        }

Open in new window


12.PNG
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 41890725
Thanks Guys. I will try out the solution tomorrow.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41890730
Sure brgdotnet. !!

Regards,
Pawan
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now