We help IT Professionals succeed at work.
Get Started

programmatically create gridview

iftech
iftech asked
on
1,753 Views
Last Modified: 2012-05-09
Hi,

I would like to duplicate the look and functionality of the ASP.Net gridview below  programmatically. Can you tell me how please?  
<asp:GridView ID="gvDocs" runat="server"
                Visible="true"
                AllowSorting="true"
                AllowPaging="true"
                PagerSettings-Mode="NumericFirstLast"
                PageSize="20"
                OnPageIndexChanging="gvDocs_PageIndexChanging"
                OnSorting="gvDocs_Sorting"
                AutoGenerateColumns="False"  
                OnRowCancelingEdit="gvDocs_RowCancelingEdit" 
                OnRowEditing="gvDocs_RowEditing" 
                OnRowUpdating="gvDocs_RowUpdating"
                OnRowDeleting="gvDocs_RowDeleting"
                OnRowDataBound="gvDocs_RowDataBound"
                ShowFooter="False" 
                DataKeyNames="PatientDocID, ProfessionalID" 
                CellPadding="4" 
                GridLines="None"
                Width="100%">
                 <Columns>
                    <asp:TemplateField HeaderText="Quadrant" SortExpression="Quadrant" HeaderStyle-Width="70px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                        <EditItemTemplate>
                            <asp:Label ID="lblQuadrant" runat="server" Text='<%# Bind("Quadrant") %>' />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblQuadrant" runat="server" Text='<%# Bind("Quadrant") %>' />
                        </ItemTemplate>
                        <ItemStyle CssClass="gridData" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Document" SortExpression="FileName" HeaderStyle-Width="100px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                        <EditItemTemplate>
                            <asp:Label ID="lblEditFileName" runat="server" Text='<%# Bind("FileName") %>' />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:HyperLink ID="hyprGridDocLink" runat="server" Text='<%# Eval("filename") %>' NavigateUrl='<%# "~/documents/" + Eval("folder") +"/" + Eval("filename") %>' Target="_blank" />
                        </ItemTemplate>
                        <ItemStyle CssClass="gridData" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Uploaded By" SortExpression="UploadedBy" ItemStyle-Width="100px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                        <EditItemTemplate>
                            <asp:Label ID="lblEditUploadedBy" runat="server" Text='<%# Bind("UploadedBy") %>' />                           
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblUploadedBy" runat="server" Text='<%# Bind("UploadedBy") %>' />
                        </ItemTemplate>
                        <ItemStyle CssClass="gridData" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Right">
                        <EditItemTemplate>
                            <asp:ImageButton ID="imgbtnUpdate" runat="server" CausesValidation="true" ValidationGroup="vgEditDoc"
                                CommandName="Update" ImageUrl="~/images/sub/saveAlt.gif"/><asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/sub/cancelAlt.gif"/>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtnEdit" runat="server" CausesValidation="false" 
                                CommandName="Edit" ImageUrl="~/images/sub/edit.gif"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" SortExpression="Description" ItemStyle-Width="220px" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" >
                        <EditItemTemplate>
                            <asp:TextBox ID="txtDescription" runat="server" Width="220px" Text='<%# Bind("Description") %>'/>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>' />
                        </ItemTemplate>
                        <ItemStyle CssClass="gridData" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date" SortExpression="UploadDate" HeaderStyle-Width="100px" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
                        <EditItemTemplate>
                            <asp:Label ID="lblEditDate" runat="server" Text='<%# string.Format("{0:MMM dd, yyyy}", Eval("UploadDate")) %>' />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDate" runat="server" Text='<%# string.Format("{0:MMM dd, yyyy}", Eval("UploadDate")) %>' />
                        </ItemTemplate>
                        <ItemStyle CssClass="gridData" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Remove" ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center">
                        <EditItemTemplate>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtnRemove" runat="server" CausesValidation="false" 
                                CommandName="Delete" ImageUrl="~/images/sub/delete.gif"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                 </Columns>
                 <PagerStyle BackColor="#595959 " Font-Bold="True" ForeColor="White" HorizontalAlign="Center"  />
                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" HorizontalAlign="Center" />
                 <EditRowStyle BackColor="#999999" VerticalAlign="Top" HorizontalAlign="Center" />
                
                 <AlternatingRowStyle CssClass="altRow" />
                 <RowStyle CssClass="defaultRow" />
                 <HeaderStyle CssClass="title" />
             </asp:GridView>

Open in new window

Comment
Watch Question
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE