Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a dataset which i am pivoting and then binding to a grid view. It is working fine when all the itemtemplates value are present in the pivoted table but in case if any of the column is not present in the pivoted table, then at the time of binding the gridview , i am getting error . Please help me that how i will handle it in grid view if any of the columns is not present but i am binding it to grid view itemtemplate. Below is my grid view code.

ASP.NET
<asp:GridView ID="gvCoreUtilization" runat="server" BackColor="White" BorderColor="#cEcFcE"
                        BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" OnRowCreated="grdPivot3_RowCreated"
                        AutoGenerateColumns="false" OnRowDataBound="grdCoreUtilization_RowDataBound">
                        <RowStyle BackColor="#F7F7DE" />
                        <FooterStyle BackColor="#CCCC99" />
                        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Label ID="lblRoleID" Text='<%#Eval("RoleId") %>' runat="server" Visible="false"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    SupervisorName
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblSupervisorName" Text='<%#Eval("SupervisorName") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    UserECode
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblUserECode" Text='<%#Eval("UserECode") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    UserName
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblUserName" Text='<%#Eval("UserName") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Designation
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblDesignation" Text='<%#Eval("Designation") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    L & D Training%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblLDTraining" Text='<%#Eval("L & D Training%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Non Production%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblNonProduction" Text='<%#Eval("Non Production%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Process Support%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblProcessSupport" Text='<%#Eval("Process Support%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Process Training%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblProcessTraining" Text='<%#Eval("Process Training%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Production%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblProduction" Text='<%#Eval("Production%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    System Downtime%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblSystemDowntime" Text='<%#Eval("System Downtime%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField>
                                <HeaderTemplate>
                                    Grand Total%
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblGrandTotal" Text='<%#Eval("Grand Total%") %>' runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>




Actually "L & D Training%","Non Production%","Process Support%","Process Training%","Production%","System Downtime%","Grand Total%" are the pivoted columns which i am binding to itemtemplate .For some User few of these column are not present and while binding the grid i am getting error
Posted
Updated 2-May-13 19:20pm
v2
Comments
Thanks7872 3-May-13 1:56am    
Show your code file.Mention clearly at which line you are getting error?whats the error?

Don't directly bind using Eval in design view instead of that You can bing data in RowDataBound event

So in RowDataBound event find label from Grid and also DataBind column from data source
If column available then bind it with label as leave it as it is.

If you are using DataTable for DataSource then you can use below code to check whether given column/property is exists or not in RowDataBound event.

C#
var dataRow = (DataRowView)e.Row.DataItem;
               var columnNameToCheck = "Id";
               var check = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheck, StringComparison.InvariantCultureIgnoreCase));
               if (check)
               {
                   // Property available
               }


Here, columnNameToCheck is variable to hold property name to check whether it is exists in data source or not.
 
Share this answer
 
v2
Comments
Dharmenrda Kumar Singh 3-May-13 2:10am    
Do you have any demo code for it so that i will also follow the same?
vijay__p 3-May-13 4:37am    
What are you using as data source? DataTable/ IEnumerable of any Class?
vijay__p 3-May-13 4:46am    
Check my updated answer for code snippet.
ganesh16 9-May-14 23:05pm    
how can i assign value to textbox from datasource...??
Please help me...
Thank you so much for this answer....
Bind these values in Rowdatabound of grid view
Sample code is show below

C#
protected void grdAyurvedic_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 Label lbl = (Label)e.Row.FindControl("LabelId");
                 lbl.Text = "Text";
            }

    }
 
Share this answer
 
C#
// ---- GetColumnIndexByHeaderText ----------------------------------
        //
        // pass in a GridView and a Column's Header Text
        // returns index of the column if found
        // returns -1 if not found 
    
    
        static public int GetColumnIndexByHeaderText(GridView aGridView, String ColumnText)
        {
            TableCell Cell;
            for (int Index = 0; Index < aGridView.HeaderRow.Cells.Count; Index++)
            {
                Cell = aGridView.HeaderRow.Cells[Index];
                if (Cell.Text.ToString() == ColumnText)
                    return Index;
            }
            return -1;
        }
    
    
        // ---- GetColumnIndexByDBName ----------------------------------
        //
        // pass in a GridView and a database field name
        // returns index of the bound column if found
        // returns -1 if not found 
    
    
        static public int GetColumnIndexByDBName(GridView aGridView, String ColumnText)
        {
            System.Web.UI.WebControls.BoundField DataColumn;
    
    
            for (int Index = 0; Index < aGridView.Columns.Count; Index++)
            {
                DataColumn = aGridView.Columns[Index] as System.Web.UI.WebControls.BoundField;
    
    
                if (DataColumn != null)
                {
                    if (DataColumn.DataField == ColumnText)
                        return Index;
                }
                }
            return -1;
        }


Reference: http://forums.asp.net/p/1095706/1653611.aspx#1653611[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900