|
-
Feb 15th, 2003, 07:12 PM
#1
Thread Starter
Frenzied Member
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
-
Feb 17th, 2003, 08:26 AM
#2
Hyperactive Member
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...
-
Feb 24th, 2003, 01:09 PM
#3
Thread Starter
Frenzied Member
Actually, I am having problems solving this. Some code to demonstrate it would be nice.
kind regards
Henrik
-
Feb 24th, 2003, 02:05 PM
#4
Hyperactive Member
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:
<asp:datagrid id="dgAuthUser" runat="server" AutoGenerateColumns="False" DataKeyField="UserId">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn Visible="False" DataField="UserId" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="UserName" ReadOnly="True" HeaderText="User"></asp:BoundColumn>
<asp:BoundColumn DataField="LastUseDate" ReadOnly="True" HeaderText="Last Login" DataFormatString="{0:ddd, dd MMM yyyy HH:mm}"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id=Checkbox1 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Role1") %>' Enabled="False">
</asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox id=Checkbox2 runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Role1") %>'>
</asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
-
Feb 25th, 2003, 09:34 AM
#5
Thread Starter
Frenzied Member
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
-
Feb 25th, 2003, 10:20 AM
#6
Hyperactive Member
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:
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
-
Feb 26th, 2003, 05:24 AM
#7
Thread Starter
Frenzied Member
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
-
Feb 26th, 2003, 06:33 AM
#8
Hyperactive Member
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?
-
Feb 26th, 2003, 07:25 AM
#9
Thread Starter
Frenzied Member
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
-
Feb 12th, 2004, 05:59 AM
#10
Addicted Member
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.
-
Mar 2nd, 2004, 03:25 PM
#11
Hyperactive Member
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.
-
Nov 17th, 2005, 06:39 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|