Need help with binding

Last post 07-24-2007 8:11 AM by lost_benny. 5 replies.

Sort Posts:

  • Need help with binding

    07-23-2007, 5:37 AM
    • Loading...
    • lost_benny
    • Joined on 07-12-2007, 4:51 AM
    • Posts 46

    Hi guys,

    I'm really stuck on this one, any advice would be great!

    I have a DataTable I constructed programmatically in my CodeBehind page. It did not reference a database in its construction. It consists of both string and bool field types. I want to be able to show the contents to the user. Once shown I wish the user to be able to check/uncheck the check boxes that represent the bool fields and have each change reflected in the DataTable.

    If I bind the DataTable directly to a GridView it is un-editable; I cannot change the checkboxes.

     

    Please again any help would be great as I'm pulling out hair at this one!

  • Re: Need help with binding

    07-23-2007, 7:10 AM
    Answer

    check out the below example i made....

    ASPX CODE:

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="test" HeaderText="test" />
                        <asp:TemplateField HeaderText="checkbox">
                            <ItemTemplate>
                                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"chkSelect")%>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

     

    ASPX.CS CODE:

    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("test",typeof(string)));
                dt.Columns.Add(new DataColumn("chkSelect", typeof(bool)));
                DataRow dr = dt.NewRow();
                dr["test"] = "kaushal";
                dr["chkSelect"] = ((bool)true);
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["test"] = "naresh";
                dr["chkSelect"] = ((bool)false);
                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }

     

    hope you got some idea....

    Thanx,
    [KaushaL] || BloG || Profile

  • Re: Need help with binding

    07-23-2007, 7:40 AM
    • Loading...
    • lost_benny
    • Joined on 07-12-2007, 4:51 AM
    • Posts 46

     Hey Kaushalparik27,

    Thanks for your help!!! That code worked in that it displays correctly in the GridView and I am able to check / uncheck the checkboxes. I am just wondering if I check / uncheck a box is the change bein reflected in the DataTable or do I have to create some function to handle this on the change event?

     

    thanks again for your code, it is really helpful :) 

  • Re: Need help with binding

    07-23-2007, 12:07 PM
    Answer
    • Loading...
    • lost_benny
    • Joined on 07-12-2007, 4:51 AM
    • Posts 46

    Hi again,

    Using the code provided above I now have a DataTable with eight rows and 13 columns. This is displayed in a GridView with the checkboxes checked for any bool value that had a value of true in the DataTable. The user can check/uncheck these checkboxes and click a submit button. Each CheckBox has an ID and a CheckedChanged event associated with it, as shown below:

    1    <asp:TemplateField HeaderText="EN">
    2       <ItemTemplate>
    3           <asp:CheckBox ID="cbxEN" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"EN")%>' OnCheckedChanged="cbxEN_CheckedChanged" />
    4       </ItemTemplate>
    5    </asp:TemplateField>
    6    <asp:TemplateField HeaderText="FR">
    7       <ItemTemplate>
    8           <asp:CheckBox ID="cbxFR" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"FR")%>' OnCheckedChanged="cbxFR_CheckedChanged"/>
    9       </ItemTemplate>
    10   </asp:TemplateField

    But as there are 8 rows does this mean the ID's are repeated? In the CheckedChanged function how would I loop through each row and change the corresponding value in the DataTable to reflect the user made changes?


  • Re: Need help with binding

    07-23-2007, 12:20 PM
    Answer
    • Loading...
    • sanraj84
    • Joined on 07-20-2007, 6:57 PM
    • Posts 48
     
    <asp:TemplateColumn HeaderText="CheckBoxField">
    <ItemTemplate>
    <asp:Label ID="lblID" Runat=server Visible=False Text='<%# DataBinder.Eval(Container.DataItem, "id")%>'>
    </asp:Label>
    <asp:CheckBox id="chkNotified" AutoPostBack=True Visible="true" OnCheckedChanged="chkNotified_CheckedChanged" runat="server"
                                            Checked='<%# DataBinder.Eval(Container.DataItem, "id") %>'  />
    </ItemTemplate>
    </asp:TemplateColumn>

     

         public void chkNotified_CheckedChanged(object sender, System.EventArgs e)
    {
    int idx;
    bool chked;
    CheckBox chkBox = (CheckBox)sender;

    foreach (DataGridItem oItem in dataGrid.Items)
    {
    CheckBox chkBox2 = (CheckBox)oItem.FindControl("chkNotified3");
    if (chkBox2.Equals(chkBox))
    {
    idx = Convert.ToInt32(((Label)oItem.FindControl("lblID3")).Text);

    if (chkBox2.Checked == true)
    chked = true;
    else
    chked = false;
                        //now write the code to update the database
                          
     
      
                    }
    }
    }
     

     

  • Re: Need help with binding

    07-24-2007, 8:11 AM
    • Loading...
    • lost_benny
    • Joined on 07-12-2007, 4:51 AM
    • Posts 46

    Hi guys,

    First thanks a lot to Kaushal and Sanraj fro all your help. I got It working! :) I now have a DataTable built programmatically whose information is displayed in a GridView. The user can change checkboxes relating to the DataTable's fields of type bool and this is reflected in the DataTable through overriding the CheckedChanged event. My code is below in case anyone is interested. Thanks again for all the help!

     

     Default.aspx

    1    <asp:GridView ID="BuildsGV" runat="server" >
    2      <Columns>
    3        <asp:TemplateField HeaderText="EN">
    4            <ItemTemplate>                    
    5                <asp:CheckBox ID="EN" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"EN")%>' OnCheckedChanged="cbx_CheckedChanged" />
    6            </ItemTemplate>
    7        </asp:TemplateField>
    8        <asp:TemplateField HeaderText="FR">
    9            <ItemTemplate>
    10               <asp:CheckBox ID="FR" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem,"FR")%>' OnCheckedChanged="cbx_CheckedChanged"/>
    11           </ItemTemplate>
    12      </asp:TemplateField>
    13     </Columns>
    14   </asp:GridView>
    15   
    
     

     Code Behind Page

     

     

    1    protected void cbx_CheckedChanged(object sender, EventArgs e)
    2    {
    3            //the checkbox that triggered the change event.
    4            CheckBox cbx = (CheckBox)sender;        
    5            CheckBox currBox = new CheckBox();
    6            DataRow editRow = confirmationData.Rows[0];
    7            int numRows = 0;
    8    
    9            //loop through the rows.
    10           foreach(GridViewRow item in BuildsGV.Rows)
    11           {
    12               currBox = (CheckBox)item.FindControl("EN");
    13   
    14               if (currBox.Equals(cbx))
    15               {
    16                   //update the corresponding language field in the DataTable.
    17                   editRow = confirmationData.Rows[numRows];
    18                   editRow["EN"] = cbx.Checked;
    19   
    20                   //match found, no need to search any further.            
    21                   break;
    22               }
    23   
    24               currBox = (CheckBox)item.FindControl("FR");
    25   
    26               if (currBox.Equals(cbx))
    27               {
    28                   //update the corresponding language field in the DataTable.
    29                   editRow = confirmationData.Rows[numRows];
    30                   editRow["FR"] = cbx.Checked;
    31   
    32                   //match found, no need to search any further.            
    33                   break;
    34               }
    35   
    36               numRows++;
    37           }
    38   }
    
      
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter