Keeping track of controls in a DataGrid

  • Thread starter Thread starter Manuel
  • Start date Start date
M

Manuel

I have the following DataGrid:

<asp:datagrid id="grdPackages" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="ShipDate" SortExpression="ShipDate"
HeaderText="Ship Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Confirm">
<ItemTemplate>
<asp:Button id="btnConfirm" runat="server" Text="Confirm"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


How do I add functionality to the btnConfirm? I want to calculate another
value based on the ShipDate [and store it in a database], how do I get the
date of the row?
 
Manuel said:
I have the following DataGrid:

<asp:datagrid id="grdPackages" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="ShipDate" SortExpression="ShipDate"
HeaderText="Ship Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Confirm">
<ItemTemplate>
<asp:Button id="btnConfirm" runat="server"
Text="Confirm"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


How do I add functionality to the btnConfirm? I want to calculate another
value based on the ShipDate [and store it in a database], how do I get the
date of the row?

Use the ItemDataBound event of the DataGrid.

John Saunders
 
I don't quite understand how the ItemDataBound event helps here.

Could you give a small example? let's say when the user click on any of
the btnConfirm buttons, the button text will become the corresponding
ShipDate.

Thanks

John said:
Manuel said:
I have the following DataGrid:

<asp:datagrid id="grdPackages" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="ShipDate" SortExpression="ShipDate"
HeaderText="Ship Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Confirm">
<ItemTemplate>
<asp:Button id="btnConfirm" runat="server"
Text="Confirm"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


How do I add functionality to the btnConfirm? I want to calculate another
value based on the ShipDate [and store it in a database], how do I get the
date of the row?

Use the ItemDataBound event of the DataGrid.

John Saunders
 
theguyver said:
I don't quite understand how the ItemDataBound event helps here.

Could you give a small example? let's say when the user click on any of
the btnConfirm buttons, the button text will become the corresponding
ShipDate.

Sorry, I see that I misunderstood what you meant when you asked about adding
functionality. You don't want to add functionality to the button, you want
to add a server-side event to the button. You can use:

<ItemTemplate>
<asp:Button id="btnConfirm" runat="server" Text="Confirm"
OnClick="btnConfirm_Click"></asp:Button>
</ItemTemplate>

and in your codebehind you can have:

Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Now, the trick is to find the row so that you can find the ship date. Now,
your button is inside of a TableCell which is inside of a DataGridItem. If
you cast it to Button, you can follow the parents:

Dim btnConfirm As Button = DirectCast(sender, Button)
Dim item As DataGridItem = DirectCast(btnConfirm.Parent.Parent,
DataGridItem)

That item allows you several ways to get to your ship date, depending on the
details of your application. For one, DataGridItem has an ItemIndex
property. You can use this to index into the DataKeys collection of the
DataGrid, assuming you supplied a DataKey to the DataGrid:

' Assumes Integer primary key
Dim pk As Integer = DirectCast(grdPackages.DataKeys(item.ItemIndex),
Integer)

You can then use the pk to fetch the data for this row from the database.
Alternatively, you can look in item.Cells(0) for the Label control
containing the ship date, and parse the Text property.

John Saunders

John said:
Manuel said:
I have the following DataGrid:

<asp:datagrid id="grdPackages" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="ShipDate" SortExpression="ShipDate"
HeaderText="Ship Date"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Confirm">
<ItemTemplate>
<asp:Button id="btnConfirm" runat="server"
Text="Confirm"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


How do I add functionality to the btnConfirm? I want to calculate another
value based on the ShipDate [and store it in a database], how do I get the
date of the row?

Use the ItemDataBound event of the DataGrid.

John Saunders
 
Back
Top