Solved

Binding a check box in an Asp.net DataGrid

Posted on 2016-11-15
4
38 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 23

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 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 23

Expert Comment

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

Regards,
Pawan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

10 Experts available now in Live!

Get 1:1 Help Now