?
Solved

Binding a check box in an Asp.net DataGrid

Posted on 2016-11-15
4
Medium Priority
?
109 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 600 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 1400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

752 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