Link to home
Start Free TrialLog in
Avatar of iftech
iftech

asked on

programmatically create gridview

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

ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iftech
iftech

ASKER

Thanks for the response.

I looked over the links and decided not to do it at run time. My issue is:
I have a repeater that with a datatable datasource. The repeater has 2 items.
1- is a date field label for the date documents were uploaded.
2- the other is a gridview contain information about the uploaded file. eg. link to file, name, discription.
some of the fields in the gridview must be editable.
The gridview won't go into edit mode and won't refresh after a file has been removed. I have included the asp.net markup and my event handlers. Can you see if I'm
missing anything.

Thanks alot.
<table width="100%" border="0" cellpadding="2" cellspacing="0">                            
                    <asp:Repeater ID="rptUploadedDocuments" runat="server" OnItemDataBound="rptUploadedDocuments_RowDataBound">
                        <ItemTemplate>
                            <tr class="subTitle">
                                <td  style="text-align:left;">
                                    <asp:ImageButton ID="imgbtnEdit" runat="server" CausesValidation="false" ImageUrl="~/images/sub/+.gif"/>
                                    <asp:Label ID="lblUploadDate" runat="server"  Text='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"UploadDate")).ToString("MMM dd, yyy") %>' />                                    
                                </td>
                            </tr>
                            <tr class="defaultRow">
                                <td>
                                 <asp:Panel ID="plDocs" runat="server" >
                                    <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="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>
                                     </asp:Panel>
                                </td>
                            </tr>
                            
                       </ItemTemplate>
                       <AlternatingItemTemplate>
                            <tr class="subTitle">
                                 <td style="text-align:left;">
                                    <asp:ImageButton ID="imgbtnEdit" runat="server" CausesValidation="false" CommandName="Edit" ImageUrl="~/images/sub/+.gif"/>
                                    <asp:Label ID="lblUploadDate" runat="server"  Text='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"UploadDate")).ToString("MMM dd, yyy") %>' />
                                 </td>
                            </tr>                            
                            <tr class="altRow">
                                <td>
                                    <asp:Panel ID = "plDocs" runat="server">
                                    <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="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>
                                    </asp:Panel> 
                                </td>
                            </tr> 
                                                      
                       </AlternatingItemTemplate>
                    </asp:Repeater>
                  </table> 

================================
C# code
================================
// loading dates into repeater
    private void GetUploadedDocumentsByDates()
    {
        PatientDocsBO uploadedDocsBO = new PatientDocsBO();
        uploadedDocsBO.PatientID = patientBO.PatientID;
        rptUploadedDocuments.DataSource = uploadedDocsBO.FetchDistinctDocdatesByPatientID();
        rptUploadedDocuments.DataBind();
        lblFileTypes.Text = Configurations.AllowedFiles;
    }
// loading gridview from repeater date
    protected void rptUploadedDocuments_RowDataBound(object sender, RepeaterItemEventArgs e)
    {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                GridView gvDocs = (GridView)e.Item.FindControl("gvDocs");

                Label lblUploadDate = (Label)e.Item.FindControl("lblUploadDate");

                string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MM-dd");
                DateTime UploadDate = Convert.ToDateTime(strDate);
                FillDocsInGrid(gvDocs, UploadDate);
                
            }
    }

// Event handlers for gridview
private void FillDocsInGrid(GridView gvDocs, DateTime uploadDate)
    {
        PatientDocsBO docsBO = new PatientDocsBO();
        DataTable dtDocs = new DataTable();
        docsBO.PatientID = Convert.ToInt32(Request.QueryString["id"].ToString());
        docsBO.UploadDate = uploadDate;
        dtDocs = docsBO.FetchActivePatientDocsByPatientIDAndUploadDate();

        if (dtDocs.Rows.Count > 0)
        {
            gvDocs.DataSource = dtDocs;
            gvDocs.DataBind();
        }
        else
        {
            dtDocs.Rows.Add(dtDocs.NewRow());
            gvDocs.DataSource = dtDocs;
            gvDocs.DataBind();

            int TotalColumns = gvDocs.Rows[0].Cells.Count;
            gvDocs.Rows[0].Cells.Clear();
            gvDocs.Rows[0].Cells.Add(new TableCell());
            gvDocs.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            gvDocs.Rows[0].Cells[0].Text = "No documents to list";
        }
    }

    protected void gvDocs_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gvDocs = (GridView)sender;
        gvDocs.EditIndex = e.NewEditIndex;
        //string a = gvDocs.ClientID;
        Label lblUploadDate = (Label)gvDocs.Parent.FindControl("lblUploadDate");
        string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MMM-dd");
        DateTime UploadDate = Convert.ToDateTime(strDate);
        SortFill(gvDocs, UploadDate);
    }

    protected void gvDocs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gvDocs = (GridView)sender;
        Label lblUploadDate = (Label)gvDocs.Parent.FindControl("lblUploadDate");
        string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MM-dd");
        DateTime UploadDate = Convert.ToDateTime(strDate);
        gvDocs.EditIndex = -1;
        SortFill(gvDocs, UploadDate);
    }

 private void SortFill(GridView gvDocs, DateTime uploaddate)
    {
        if (ViewState["dt" + gvDocs.ClientID] == null)
            FillDocsInGrid(gvDocs, uploaddate);
        else
        {
            
            gvDocs.DataSource = (DataTable)ViewState["dt"+ gvDocs.ClientID];
            gvDocs.DataBind();
        }
    }

    protected void gvDocs_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PatientDocsBO docBO = new PatientDocsBO();
        //lblEditSubMsg.Text = "";
        GridView gvDocs = (GridView)sender;
        Label lblUploadDate = (Label)gvDocs.Parent.FindControl("lblUploadDate");
        string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MM-dd");
        DateTime UploadDate = Convert.ToDateTime(strDate);

        TextBox txtDescription = (TextBox)gvDocs.Rows[e.RowIndex].FindControl("txtDescription");

        docBO.PatientDocID = Convert.ToInt32(gvDocs.DataKeys[e.RowIndex].Values[0].ToString());
        docBO.FetchPatientDocByID();

        docBO.Description = txtDescription.Text;
        docBO.UpdatePatientDoc();

        gvDocs.EditIndex = -1;

        FillDocsInGrid(gvDocs, UploadDate);
    }

    protected void gvDocs_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        PatientDocsBO docBO = new PatientDocsBO();
        GridView gvDocs = (GridView)sender;
        Label lblUploadDate = (Label)gvDocs.Parent.FindControl("lblUploadDate");
        string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MM-dd");
        DateTime UploadDate = Convert.ToDateTime(strDate);
        docBO.PatientDocID = Convert.ToInt32(gvDocs.DataKeys[e.RowIndex].Values[0].ToString());
        docBO.FetchPatientDocByID();

        docBO.Active = false;
        docBO.UpdatePatientDoc();

        gvDocs.EditIndex = -1;

        FillDocsInGrid(gvDocs, UploadDate);
    }

    protected void gvDocs_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gvDocs = (GridView)sender;
        Label lblUploadDate = (Label)gvDocs.Parent.FindControl("lblUploadDate");
        string strDate = Convert.ToDateTime(lblUploadDate.Text).ToString("yyyy-MM-dd");
        DateTime UploadDate = Convert.ToDateTime(strDate);
        FillDocsInGrid(gvDocs, UploadDate);
        GridViewSortExpression = e.SortExpression;
        int pageIndex = gvDocs.PageIndex;
        gvDocs.DataSource = SortDataTable(gvDocs.DataSource as DataTable, false);
        gvDocs.DataBind();
        gvDocs.PageIndex = pageIndex;
        ViewState["dt" + gvDocs.ClientID] = ((DataView)gvDocs.DataSource).ToTable();
    }

    protected void gvDocs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgRemove = (ImageButton)e.Row.FindControl("imgbtnRemove");
            if ((DataBinder.Eval(e.Row.DataItem, "ProfessionalID") == DBNull.Value) || (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ProfessionalID")) != SessionHandler.ProfessionalID))
            {
                //Only allow pros who uploaded the file to edit the description
                ImageButton imgEdit = (ImageButton)e.Row.FindControl("imgbtnEdit");
                if (imgEdit != null) { imgEdit.Style.Add("display", "none"); }

                //Only allow pros who uploaded the file to delete it
                if (imgRemove != null)
                {
                    imgRemove.Style.Add("display", "none");
                }
            }
            else
            {
                if (imgRemove != null) { imgRemove.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this file?')"); }
            }

        }
    }

Open in new window

One simple trick I follow while doing big projects is to break it into simple pieces. You can try that on your gridview. Have a basic gridview, make sure it renders fine. Then add the edit part for the gridview and make sure it works correctly.. and so on.

Here are some links that'll help you with an editable gridview.

http://www.codeproject.com/KB/webforms/Editable_GridView.aspx
http://msdn.microsoft.com/en-us/library/ms972948.aspx

Arun
Avatar of iftech

ASKER

I see your point. However the gridview does render correctly. Just some functionality isn't working. the events are firing but the databind seems to do nothing.

thanks for your help
Din't the links help you in any way?

Arun
Avatar of iftech

ASKER

Well they did... kind of,  the last gridview in the repeater list works as it should but the others don't.
Avatar of iftech

ASKER

I got it all to work. thanks for your help. Although I figured most of it out myself but I will award you the points.