DataGrid column for updating values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I know datagrid support updating values in a given record.

But is it at all possible for the editing to NOT use a TextBox, but instead,
render a dropdown list (from a lookup table). If so, How can I define the
<Columns/> <asp:BoundColumn/>, etc.?
 
You can use a DropDownList via a TemplateCOlumn:

ie <asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Status") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlStatus" runat="server">
<asp:ListItem Value="1">Active</asp:ListItem>
<asp:ListItem Value="0">Inactive</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Where "Status" is the name of the field in a dataset that I bound to. Note
that I hard coded in the values in the dropdownlist, but this can be
databound just like any other list - the easiest way to do it is to just
create the list, set the properties, and then paste over the ListItem code
that I had here.

Let me know if you have any problems, I'll be glad to help if you do but I
think this should get you what you need.
 
Hi,

In additional to "W.G. Ryan eMVP"'s reply, there are 2 great articles talks
about customizing Asp.net DataGrid columns, for your information:
"Creating Custom Columns for the ASP.NET Datagrid"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/creatingcustomcolumns.asp
"Understanding Templates in ASP.NET"
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I followed the example at
http://msdn.microsoft.com/library/e...ns.asp?frame=true#creatingcustomcolumns_topic
http://msdn.microsoft.com/library/e...ns.asp?frame=true#creatingcustomcolumns_topic
http://msdn.microsoft.com/library/e...s.asp?frame=true#creatingcustomcolumns_topic8

Whilst I now successfully manage to get a drop down list (having a
DataReader that populates a DropDownList in page_load), I now lost
1) HeaderStyle for this column with a dropdownlist
2) sorting (No link appear for sorting)

My ASP/HTML are defined as (as you see even explicitly trying to set the
HeaderStyle within the Column does NOT work. How can I fix this?

<asp:DataGrid id="FormListDataGrid" runat="server" HeaderStyle-Height="25px"
HeaderStyle-CssClass="header"
EditItemStyle="data" ShowHeader="true" AutoGenerateColumns="False"
AllowSorting="true" OnSortCommand="SortForm_OnClick"
OnEditCommand="FormListDataGrid_Edit"
OnCancelCommand="FormListDataGrid_Cancel"
OnUpdateCommand="FormListDataGrid_Update"
DataKeyField="DueDate">
<ItemStyle CssClass="data"></ItemStyle>
<HeaderStyle Height="25px" CssClass="header"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="FormName" SortExpression="FormName"
HeaderText="Form Name"></asp:BoundColumn>
<asp:BoundColumn DataField="DueDate" SortExpression="DueDate"
HeaderText="Due Date"></asp:BoundColumn>
<dgg:DropDownColumn DataField="formType" SortExpression="formType"
HeaderText="Form Type" HeaderStyle-Height="25px"
HeaderStyle-CssClass="header"></dgg:DropDownColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />
</Columns>
</asp:DataGrid>
 
Hi,

Thanks for your feedback.

After downloading the source code of that MSDN article, I can reproduce out
the can not sorting issue.

After doing some research, I found that this is a little problem of the
implementation of that DropDownColumn. Normally, in
DataGridCell.InitializeCell() method, it internally will check if sorting
is enabled, then place a LinkButton control in the HeaderRow of that
column. However, in the implementation of DropDownColumn, it writes code
like this:

Select Case itemType
Case ListItemType.Header
cell.Text = HeaderText
Case ListItemType.Item, ListItemType.AlternatingItem
.....
End Select

As we can see that, it place a text string in the DataGridCell(cell), then
this string will in front of the LinkButton and blocked it. So we can not
see the sorting LinkButton.

To workaround this issue, we can just determine the sorting condition, then
do not place headertext for sorting, like this:

Select Case itemType
Case ListItemType.Header
If Me.Owner.AllowSorting = True And Me.SortExpression <> Nothing
And Me.SortExpression <> String.Empty Then
Else
cell.Text = HeaderText
End If
Case ListItemType.Item, ListItemType.AlternatingItem
........
End Select

After this modification, the LinkButton will appear when we enabling
sorting.

For the HeaderStyle issue, it seems that it works well on my side. For
example, if I set HeaderStyle-ForeColor="Red" in DropDownColumn, the string
text color will become red in the page, like this:

<asp:DataGrid id="DataGrid1" AllowSorting="True" runat="server"
CssClass="grid" AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="OrderID" SortExpression="OrderID"
ReadOnly="True" HeaderText="Order ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ShipName" ReadOnly="True" HeaderText="Ship
to"></asp:BoundColumn>
<asp:BoundColumn DataField="ShipCountry" ReadOnly="True"
HeaderText="Country"></asp:BoundColumn>
<dgg:DropDownColumn HeaderStyle-ForeColor="Red" SortExpression="ShipVia"
DataField="ShipVia" DataTextField="CompanyName" DataValueField="ShipperID"
HeaderText="Ship Method">
</dgg:DropDownColumn>
</Columns>
</asp:DataGrid>
==================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top