Here's an example of a Gridview, with its event:
To bind the data, you can do something like this:Code:<asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoGenerateColumns="false" BackColor="White" BorderWidth="0px" BorderStyle="Solid" CellPadding="0" EmptyDataText="No Folders" ForeColor="#333333" GridLines ="Both" Width="100%" OnSorting="GridViewSortEventHandler" ShowHeader="true" > <HeaderStyle BackColor="#d3e0ef" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="#d3e0ef" ForeColor="#284775" /> <RowStyle BackColor="white" ForeColor="#333333" /> <Columns> <asp:TemplateField SortExpression="GridViewSortEventHandler" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="10" ItemStyle-Width="20" HeaderImageUrl="~/images/up_directory_icon.gif" > <ItemTemplate> <asp:Label runat="server" ID="objValue" Visible="false" Text='<%#Eval("objectValue")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="30" HeaderText="Sel" > <ItemTemplate> <asp:CheckBox ID="cb" runat="server" OnCheckedChanged="cb_Click" AutoPostBack="true" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="50" HeaderImageUrl="~/images/folder.gif"> <ItemTemplate> <asp:ImageButton ID="selectDir" runat="server" ImageUrl="~/images/folder.gif" OnClick="selectDir_click" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="left"> <ItemTemplate> <asp:Label runat="server" ID="dirName" Text='<%#Eval("directoryName")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
In this example, I have a list of objects with corresponding columns. You can list, datasets, arrays, etc.Code:Public Sub bindData(ByVal getList As List(Of directoryList)) If (getList.Count < 1) Then Dim dl As New directoryList dl.directoryName = "" getList.Add(dl) Me.GridView1.DataSource = getList Me.GridView1.DataBind() Me.GridView1.Rows(0).Visible = False Else Me.GridView1.DataSource = getList Me.GridView1.DataBind() End If End Sub
To get a selected row, assuming this is coming from a button click, you can get the button and find its parent row
This should give you a start. if you have any questions, post back hereCode:Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand 'get the button that the user clicked. can also use linkedbutton, imagebutton, etc Dim btn As Button = DirectCast(sender, Button) ' get the gridview it was on Dim gv As GridViewRow = DirectCast(btn.Parent.Parent, GridViewRow) ' get teh string value from that controls, assuming it's called myValue Dim myButtonId As String = DirectCast(gv.FindControl("myValue"), Label).Text end sub
jason




Reply With Quote