Need query value not displayed in DataGrid

A

Anna

I have a simple DataGrid that displays a list of characteristics and
allows you to edit a description for each characteristic. The query
that feeds this involves a join, as the characteristics and their
descriptions are in separate tables. The DataKeyField of the DataGrid
is set to the description ID, but I also need to be able to access the
characteristic ID in my update query (without displaying it in the
DataGrid). I haven't had much luck coming up with a solution (I'm
pretty new to .NET). Can anyone help?

Thanks!
Anna
 
J

Joe Fallon

You have to turn off the automatic use of columns and build them by hand.
The grid Property sheet can help - but once you see how it is done, it is
just as easy to directly edit the HTML and bind only those fields you want
to display.

Keep the datasource in Session for postbacks.
When the page is posted back you can define which row in the datasource was
selected (if you have a button in each row) and then you can use that index
value to identify all the info about that row in your datasource and get the
right Characteristic ID.

e.g. Note: OnItemCommand="GetRowInfo" (this method must defined in your code
behind.)

<asp:datagrid id="dg1" runat="server" Width="95%"
HorizontalAlign="Center"
BackColor="White" CellPadding="4" AllowSorting="True"
AutoGenerateColumns="False" GridLines="None"
CellSpacing="2" AllowPaging="True" PageSize="20" EnableViewState="False"
OnItemCommand="GetRowInfo">
<AlternatingItemStyle Wrap="False"></AlternatingItemStyle>
<ItemStyle Wrap="False"></ItemStyle>
<PagerStyle NextPageText="Next" PrevPageText="Previous" Wrap="False"
Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="myName" SortExpression="myName"
HeaderText="My Name"></asp:BoundColumn>
<asp:BoundColumn DataField="sometext" SortExpression="sometext"
HeaderText="Active"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Choose Row">
<ItemTemplate>
<asp:imagebutton CommandName="btnEdit" id="btnEdit"
runat="server" ImageUrl="~/img/edit.gif" ToolTip="Edit"></asp:imagebutton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

Protected Sub GetRowInfo(ByVal source As Object, ByVal e As
DataGridCommandEventArgs)
'Grid paging/sorting causes this event to fire and e.Item.ItemIndex=-1
so we should avoid an error by coding for this case.
If e.Item.ItemIndex >= 0 Then

'get the index of the row in the grid (which is also the index for
your data source)
Dim mIndex As Integer = CType(source, DataGrid).PageSize *
CType(source, DataGrid).CurrentPageIndex + e.Item.ItemIndex

If e.CommandName = "btnEdit" Then
Session("myKey") = myList(mIndex).myKey
'the page below expects a value of myKey to be in Session since you
chose it from the page with the grid.
Response.Redirect("~/MyEditPage.aspx")
End If

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top