Wednesday 5 June 2013

Grid binding code


 <asp:GridView ID="gvRInProgress" runat="server" Width="850px" CellPadding="4" EnableModelValidation="True"
                            ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" OnRowCommand="gvRInProgress_RowCommand"
                            EmptyDataText="In Progress Data not found." AllowPaging="True" OnPageIndexChanging="gvRInProgress_PageIndexChanging">
                            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" />
                            <Columns>
                                <asp:TemplateField HeaderText="Source">
                                    <ItemTemplate>
                                        <asp:Label ID="lblListName" runat="server" Width="100px" Text='<%# DataBinder.Eval(Container.DataItem, "List") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Job Description">
                                    <ItemTemplate>
                                        <asp:Label ID="lblId" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:Label>
                                        <asp:LinkButton ID="lnkbtnViewItem" runat="server" Width="150px" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                                            CommandName="ViewItem"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Comments History">
                                    <ItemTemplate>
                                        <asp:Label ID="lblCommentsHistory" Width="250px" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Comments") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Comments">
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtComments" runat="server" Width="100px"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:Button ID="btnSendbacktocustomer" runat="server" CommandName="SendBack" Width="100px"
                                            Text="SendBack" />
                                        <asp:Button ID="btnSaveComments" runat="server" CommandName="SaveComments" Width="100px"
                                            Text="Save" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EditRowStyle BackColor="#7C6F57" HorizontalAlign="Left" />
                            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#E3EAEB" HorizontalAlign="Left" />
                            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" HorizontalAlign="Left" />
                        </asp:GridView>


  private void BindData(String strStatus)
        {
            DataTable dtGlobal = new DataTable();
            DataRow drGlobal;

            //Data table column declaration
            dtGlobal.Columns.Add("List");
            dtGlobal.Columns.Add("Id");
            dtGlobal.Columns.Add("Title");
            dtGlobal.Columns.Add("Comments");
            using (SPSite oCurrentSite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb oCurrentWeb = oCurrentSite.OpenWeb())
                {

                    SPList oListResource = null;
                    SPList oListTools = null;
                    SPList oListSolution = null;
                    SPList oListExecution = null;

                    oListResource = oCurrentWeb.Lists["Service Request"];
                    oListTools = oCurrentWeb.Lists["Request for Tools"];
                    oListSolution = oCurrentWeb.Lists["Request for Solution"];
                    oListExecution = oCurrentWeb.Lists["Request for Execution"];


                    String queryResource = "";
                    String queryTools = "";
                    String querySolution = "";
                    String queryExecution = "";
                    if (strStatus == "Inprogress")
                    {
                        queryResource = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Business_x0020_Unit'/><Value Type='Text'>" + ViewState["GroupName"] + "</Value></Eq></And></Where>";
                        queryTools = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Business_x0020_Unit'/><Value Type='Text'>" + ViewState["GroupName"] + "</Value></Eq></And></Where>";
                        querySolution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Business_x0020_Unit'/><Value Type='Text'>" + ViewState["GroupName"] + "</Value></Eq></And></Where>";
                        queryExecution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Business_x0020_Unit'/><Value Type='Text'>" + ViewState["GroupName"] + "</Value></Eq></And></Where>";
                    }
                    else if (strStatus == "Pending")
                    {
                        queryResource = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        queryTools = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        querySolution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        queryExecution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                    }
                    else if (strStatus == "Completed")
                    {
                        queryResource = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        queryTools = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        querySolution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                        queryExecution = @"<Where><And><Eq><FieldRef Name='Status' /><Value Type='Text'>" + strStatus + "</Value></Eq><Eq><FieldRef Name='Author'/><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name.ToString() + "</Value></Eq></And></Where>";
                    }

                    queryResource = String.Format(queryResource, "Status");
                    queryTools = String.Format(queryTools, "Status");
                    querySolution = String.Format(querySolution, "Status");
                    queryExecution = String.Format(queryExecution, "Status");

                    SPQuery spQueryResource = new SPQuery();
                    SPQuery spQueryTools = new SPQuery();
                    SPQuery spQuerySolution = new SPQuery();
                    SPQuery spQueryExecution = new SPQuery();

                    spQueryResource.Query = queryResource;
                    spQueryTools.Query = queryTools;
                    spQuerySolution.Query = querySolution;
                    spQueryExecution.Query = queryExecution;

                    SPListItemCollection itemsResource = oListResource.GetItems(spQueryResource);
                    SPListItemCollection itemsTools = oListTools.GetItems(spQueryTools);
                    SPListItemCollection itemsSolution = oListSolution.GetItems(spQuerySolution);
                    SPListItemCollection itemsExecution = oListExecution.GetItems(spQueryExecution);

                    DataTable dtResource = new DataTable();
                    DataTable dtTools = new DataTable();
                    DataTable dtSolution = new DataTable();
                    DataTable dtExecution = new DataTable();

                    dtResource = itemsResource.GetDataTable();
                    dtTools = itemsTools.GetDataTable();
                    dtSolution = itemsSolution.GetDataTable();
                    dtExecution = itemsExecution.GetDataTable();

                    

     gvRInProgress.DataSource = dtResource;
                    gvRInProgress.DataBind();

No comments:

Post a Comment