Solved

DataGrid and CheckList Binding

Posted on 2004-04-21
2
988 Views
Last Modified: 2008-02-01
Hi All,

W have requirement like this:

We have a datagrid which display the data from a DBUsers Table.

Each row in the datagrid will have a "Edit" link, and on click of  "Edit", the row data will become editable and "Update" and "Cancel" link gets displayed for that row instead of "Edit" link.

One of the column data returns the access codes in the form of "1" and "0", for example 10001. Each characters represent whether user has got access ( if its "1") or not( if its "0") for a particular operation i.e. add, delete, update etc etc. I want to display the this column value in a <ASP:CheckBoxList> control, so when user clicks on "Edit" link, the access code value "10001", will be displayed as five checkboxes.

I could able to display the check boxes, but I wanted to know how can I check/uncheck the check box depending on the access code string. For example for access code string "10001", the first and fifth checkbox should be displayed as checked and others unchecked. How can I do that.

The code for datagrid is as as follows:

----------------------------------------------------------------------------------------------------
<form id="WebForm2" method="post" runat="server">
<ASP:DataGrid id="UserDBDataGrid" runat="server" CellPadding="2" EditItemStyle-BackColor="yellow" DataKeyField="EMAILID" OnEditCommand="OnEdit" OnUpdateCommand="OnUpdate" OnCancelCommand="OnCancel" OnDeleteCommand="OnDelete" AutoGenerateColumns="False">
<Columns>
<ASP:TemplateColumn HeaderText="Access Permission">
<ItemTemplate>
 <ASP:Label Text='<%# DisplayAccessList(Convert.ToString(DataBinder.Eval (Container.DataItem, "ACCESSCODE"))) %>' runat="server" ID="LabelAccessPerm" />
</ItemTemplate>
<EditItemTemplate>
<ASP:CheckBoxList ID="access_checkboxlist" DataValueField="Value" DataTextField="Key" DataSource='<%# GetHashAccessCode(Convert.ToString(DataBinder.Eval(Container.DataItem, "ACCESSCODE"))) %>' RepeatDirection="Horizontal" runat="server" >
</ASP:CheckBoxList>
</EditItemTemplate>
</ASP:TemplateColumn>
<ASP:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</ASP:DataGrid>
</form>
----------------------------------------------------------------------------------------------------

Here the GetHashAccessCode() function  returns HashTable name/value pair for the CheckBoxList control.

Thanks in advance,
Lilu

0
Comment
Question by:lilupat
2 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 100 total points
Comment Utility
you can do this only manually
create ItemDataBound event for the datagrid

public void dgDrafts_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
  {
    CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("access_checkboxlist");
    Label l = (Label)e.Item.FindControl("LabelAccessPerm");
     if ( cbl != null && l != null )
      {
        for ( int i=0; i < cbl.Items.Count; i++)
          {
              if ( l.Text.Substring(i,1) == "1") cbl.Items[i].Selected = true;
          }
       }
  }
}

HTH
B..M
0
 

Author Comment

by:lilupat
Comment Utility
That's great a answer. Its working fine for me . Thanks a lot ....
Lilu
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

12 Experts available now in Live!

Get 1:1 Help Now