Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Binding a check box in an Asp.net DataGrid

Posted on 2016-11-15
4
Medium Priority
?
127 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 32

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 32

Expert Comment

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

Regards,
Pawan
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
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 …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

610 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