Results 1 to 12 of 12

Thread: DataGrid content...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    DataGrid content...

    Does anyone know how to show, say a combobox in a datagrid?
    I have one value that is boolean, and I don't want the user to write "true" or "false" but rather just pick it in a combobox when changing values in the datagrid.

    Also, how is it possible to write values other than simple text? When I show the bool value, I don't want to display text, but rather a checkbox that is either checked or unchecked? Is this possible????

    kind regards
    Henrik

  2. #2
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    You want to use a template column which allows you to embed other contols. Look into it and if you need I can post some code tomorrow. Also how about using a checkbox for your solution instead of the combo. This will automatically take care of your Bool problem...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Actually, I am having problems solving this. Some code to demonstrate it would be nice.

    kind regards
    Henrik

  4. #4
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    Here's my datagrid definition.

    I've got a TemplateColumn for the checkbox and am using 2 templates (one each for edit/non-edit mode).
    Both use late-binding to get their value and reference a boolean data element.

    Hope this helps. Good luck!

    VB Code:
    1. <asp:datagrid id="dgAuthUser" runat="server" AutoGenerateColumns="False" DataKeyField="UserId">
    2.   <Columns>
    3.     <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
    4.     <asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
    5.     <asp:BoundColumn Visible="False" DataField="UserId" ReadOnly="True"></asp:BoundColumn>
    6.     <asp:BoundColumn DataField="UserName" ReadOnly="True" HeaderText="User"></asp:BoundColumn>
    7.     <asp:BoundColumn DataField="LastUseDate" ReadOnly="True" HeaderText="Last Login" DataFormatString="{0:ddd, dd MMM yyyy HH:mm}"></asp:BoundColumn>
    8.     <asp:TemplateColumn>
    9.     <ItemTemplate>
    10.       <asp:CheckBox id=Checkbox1 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Role1") %>' Enabled="False">
    11.       </asp:CheckBox>
    12.     </ItemTemplate>
    13.     <EditItemTemplate>
    14.       <asp:CheckBox id=Checkbox2 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Role1") %>'>
    15.       </asp:CheckBox>
    16.     </EditItemTemplate>
    17.     </asp:TemplateColumn>
    18.   </Columns>
    19. </asp:datagrid>

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Thanks, I somehow got it to work, but now Im struggling with the update. In the database I have the admin value stored as varchar, and I don't want to change this. But how do I write the line that get the value from the checkbox? sParams is a string array... and the line below give me error "specified cast isn't valid"

    sParams[3] = ((CheckBox)e.Item.Cells[3].Controls[0]).Text.ToString();

    I have also tried

    sParams[3] = ((CheckBox)e.Item.Cells[3].Controls[0]).Checked.ToString();

    same error...

    how can I do it??? Anyone?

    kind regards
    Henrik

  6. #6
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    I'm guessing Control[0] is not actually a checkbox.
    Set a breakpoint and you might find it to be a <TD>.

    Rather that going after it by index, how about trying the control id as in:

    VB Code:
    1. sParams[3] = CType(e.Item.FindControl("<id>"), CheckBox).Checked.ToString

    This should give you "true" or "false".

    Not that I recommend storing such values in the DB

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    I do believe you can write it like this in C#:

    ((CheckBox)e.Item.FindControl("<id>")).Checked.ToString();

    I am performing a typecast to type CheckBox... but when I run this I get

    "object reference not set to an instance of an object"! That probably means that the findcontrol is null...

    what do I do wrong really?

    kind regards
    henrik

  8. #8
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    That typecast syntax works in VB too. I just prefer the other way...

    Anyway back to your problem. I'm presuming you have substituted <id> with the id for your checkbox?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Yes, but stupid me had written "Checkbox1", which is the item checkbox, not the edititem checkbox...

    replaced with "Checkbox2" and it worked great!!


    thanks for your help!

    Henrik

  10. #10
    Addicted Member
    Join Date
    Jan 2002
    Posts
    144

    hi fungi

    i faced the same problem, with ur code i solved it when it is populated, but when i am in edit mode, i want to change the status of the check box (ie) when it checked i want to uncheck it and vice versa, it works correctly, but when i read the status of the check box after editing it always shows the first status of the check box (ie) when it is originally checked and i changed the status by unchecking it, when i read the value it shows TRUE.

    dim bValue as Boolean

    bValue = CType(e.Item.FindControl("CheckBox1"), CheckBox).Checked

    when i read the bValue it is TRUE because initially it is checked, but when i uncheck the checkbox, then also the bValue is TRUE, but it should be FALSE

    any suggestions.
    thanx.
    Last edited by senthilkbs; Feb 12th, 2004 at 06:17 AM.

  11. #11
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381

    Re: hi fungi


    bValue = CType(e.Item.FindControl("CheckBox1"), CheckBox).Checked
    You are actually reading the value of "CheckBox1" which is the original value of the check box prior to clicking the "EDIT" button on the grid. Change this to "CheckBox2" instead which would be the id of the edit template.

  12. #12
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: DataGrid content...

    I've got a couple of questions. Are you required to enter the control type of your template column in HTML? Can you dynamically add/remove rows at run time?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width