当数据表中不存在列但在itemtemplate处绑定时绑定gridview [英] Bind gridview when column not exists in datatable but binding at itemtemplate

查看:68
本文介绍了当数据表中不存在列但在itemtemplate处绑定时绑定gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,我正在转动然后绑定到网格视图。当所有itemtemplates值都存在于pivoted表中时它工作正常但是如果在pivoted表中没有任何列,那么在绑定gridview时,我收到错误。请帮助我如何在网格视图中处理它,如果任何列不存在但我绑定到网格视图itemtemplate。下面是我的网格视图代码。



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: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





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

推荐答案

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.



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.

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.



Here, columnNameToCheck is variable to hold property name to check whether it is exists in data source or not.


Bind these values in Rowdatabound of grid view

Sample code is show below



Bind these values in Rowdatabound of grid view
Sample code is show below

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

    }


// ---- 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[^]


这篇关于当数据表中不存在列但在itemtemplate处绑定时绑定gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆