Solved

Binding a check box in an Asp.net DataGrid

Posted on 2016-11-15
4
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar 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 15

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 29

Expert Comment

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

Regards,
Pawan
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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