C# 检查是否存在柱并绑定网格(如果存在)

C# 检查是否存在柱并绑定网格(如果存在),c#,asp.net,C#,Asp.net,如何在RowDataBound事件中将数据绑定到gridview的itemtemplate。我正在使用一个网格视图,下面是该网格视图的代码 <asp:GridView ID="gvCoreUtilization" runat="server" BackColor="White" BorderColor="#cEcFcE" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="

如何在
RowDataBound
事件中将数据绑定到
gridview
itemtemplate
。我正在使用一个网格视图,下面是该网格视图的代码

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

执行上述查询后,我正在对c#中的“活动”列进行透视,这些列是“L&D培训%”、“非生产%”、“流程支持%”、“流程培训%”、“生产%”、“系统停机%”、“总计%”,我绑定在ItemTemplate上

如果值为空,此代码允许您隐藏特定单元格,使其不被代码隐藏

Protected Sub gvCoreUtilization_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCoreUtilization.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then

                Dim olblRoleID As New Label
                Dim olblSupervisorName As New Label
                Dim olblUserECode As New Label
                Dim olblUserName As New Label
                Dim olblDesignation As New Label
                Dim olblLDTraining As New Label
                Dim olblNonProduction As New Label
                Dim olblProcessSupport As New Label
                Dim olblProcessTraining As New Label
                Dim olblProduction As New Label
                Dim olblSystemDowntime As New Label
                Dim olblGrandTotal As New Label

                olblRoleID = CType(e.Row.FindControl("lblRoleID"), Label)
                olblSupervisorName = CType(e.Row.FindControl("lblSupervisorName"), Label)
                olblUserECode = CType(e.Row.FindControl("lblUserECode"), Label)
                olblUserName = CType(e.Row.FindControl("lblUserName"), Label)
                olblDesignation = CType(e.Row.FindControl("lblDesignation"), Label)
                olblLDTraining = CType(e.Row.FindControl("lblLDTraining"), Label)
                olblNonProduction = CType(e.Row.FindControl("lblNonProduction"), Label)
                olblProcessSupport = CType(e.Row.FindControl("lblProcessSupport"), Label)
                olblProcessTraining = CType(e.Row.FindControl("lblProcessTraining"), Label)
                olblProduction = CType(e.Row.FindControl("lblProduction"), Label)
                olblSystemDowntime = CType(e.Row.FindControl("lblSystemDowntime"), Label)
                olblGrandTotal = CType(e.Row.FindControl("lblGrandTotal"), Label)


                If CType(DataBinder.Eval(e.Row.DataItem, "RoleId"), String) = "" Then
                    olblRoleID.Visible = False
                Else
                    olblRoleID.Text = DataBinder.Eval(e.Row.DataItem, "RoleId")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "SupervisorName"), String) = "" Then
                    olblSupervisorName.Visible = False
                Else
                    olblSupervisorName.Text = DataBinder.Eval(e.Row.DataItem, "SupervisorName")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "UserECode"), String) = "" Then
                    olblUserECode.Visible = False
                Else
                    olblUserECode.Text = DataBinder.Eval(e.Row.DataItem, "UserECode")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "UserName"), String) = "" Then
                    olblUserName.Visible = False
                Else
                    olblUserName.Text = DataBinder.Eval(e.Row.DataItem, "UserName")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Designation"), String) = "" Then
                    olblDesignation.Visible = False
                Else
                    olblDesignation.Text = DataBinder.Eval(e.Row.DataItem, "Designation")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "L & D Training"), String) = "" Then
                    olblLDTraining.Visible = False
                Else
                    olblLDTraining.Text = DataBinder.Eval(e.Row.DataItem, "L & D Training")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Non Production"), String) = "" Then
                    olblNonProduction.Visible = False
                Else
                    olblNonProduction.Text = DataBinder.Eval(e.Row.DataItem, "Non Production")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Process Support"), String) = "" Then
                    olblProcessSupport.Visible = False
                Else
                    olblProcessSupport.Text = DataBinder.Eval(e.Row.DataItem, "Process Support")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Process Training"), String) = "" Then
                    olblProcessTraining.Visible = False
                Else
                    olblProcessTraining.Text = DataBinder.Eval(e.Row.DataItem, "Process Training")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Production"), String) = "" Then
                    olblProduction.Visible = False
                Else
                    olblProduction.Text = DataBinder.Eval(e.Row.DataItem, "Production")
                End If


                If CType(DataBinder.Eval(e.Row.DataItem, "System Downtime"), String) = "" Then
                    olblSystemDowntime.Visible = False
                Else
                    olblSystemDowntime.Text = DataBinder.Eval(e.Row.DataItem, "System Downtime")
                End If

                If CType(DataBinder.Eval(e.Row.DataItem, "Grand Total"), String) = "" Then
                    olblGrandTotal.Visible = False
                Else
                    olblGrandTotal.Text = DataBinder.Eval(e.Row.DataItem, "Grand Total")
                End If

            End If
        Catch ex As Exception
            General.LogException(ex)
        End Try
    End Sub
若你们想在网格中隐藏整个列,请遵循下面的步骤

1) 在页上为每列创建视图状态属性

 Property RoleId() As boolen
        Get
            If IsNothing(Me.ViewState("RoleId")) Then Me.ViewState("RoleId") = false
            Return CType(Me.ViewState("RoleId"), Boolean)
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("RoleId") = value
        End Set
    End Property
2) 为每行的每一列循环抛出数据集,并检查值是否存在,然后设置此属性,最后您拥有值为true或false的属性,您可以找到需要在网格中显示的列

3) 然后您可以很容易地隐藏网格的列

if Me.RoleId= True then
'Write code to Display
else
'Write code to Hide
end if
if(e.Row.RowType==DataControlRowType.DataRow)
{
System.Web.UI.WebControls.Label lblRoleNo=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblRoleId”);
System.Web.UI.WebControls.Label lblSupervisorName=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblSupervisorName”);
System.Web.UI.WebControls.Label lblUserECode=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblUserECode”);
System.Web.UI.WebControls.Label lblUserName=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblUserName”);
System.Web.UI.WebControls.Label lblDesignation=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblDesignation”);
System.Web.UI.WebControls.Label lblLDTraining=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblLDTraining”);
System.Web.UI.WebControls.Label lblNonProduction=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblNonProduction”);
System.Web.UI.WebControls.Label lblProcessSupport=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblProcessSupport”);
System.Web.UI.WebControls.Label lblProcessTraining=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblProcessTraining”);
System.Web.UI.WebControls.Label lblProduction=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblProduction”);
System.Web.UI.WebControls.Label lblSystemDowntime=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblSystemDowntime”);
System.Web.UI.WebControls.Label lblGrandTotal=(System.Web.UI.WebControls.Label)e.Row.FindControl(“lblGrandTotal”);
//检查轴中是否存在气象柱
var dataRow=(DataRowView)e.Row.DataItem;
var columnNameToCheck=“L&D培训%”;
var checkTraining=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(columnNameToCheck,StringComparison.InvariantCultureIgnoreCase));
如果(检查培训)
{
//可用财产
lblLDTraining.Text=(DataBinder.Eval(e.Row.DataItem,“L&D培训%”).ToString();
}
其他的
{
lblLDTraining.Visible=false;
}
var columnNoProduction=“非生产%”;
var checkNoProduction=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(ColumnNoProduction,StringComparison.InvariantCultureIgnoreCase));
如果(检查非生产)
{
//可用财产
lblNonProduction.Text=(DataBinder.Eval(e.Row.DataItem,“非生产%”).ToString();
}
其他的
{
lblNonProduction.Visible=false;
}
var columnProcessSupport=“流程支持%”;
var checkProcessSupport=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(columnProcessSupport,StringComparison.InvariantCultureIgnoreCase));
if(checkProcessSupport)
{
//可用财产
lblProcessSupport.Text=(DataBinder.Eval(e.Row.DataItem,“流程支持%”).ToString();
}
其他的
{
lblProcessSupport.Visible=false;
}
var columnProcessTraining=“流程培训%”;
var checkProcesstraining=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(columnProcessTraining,StringComparison.InvariantCultureIgnoreCase));
if(检查流程培训)
{
//可用财产
lblProcessTraining.Text=(DataBinder.Eval(e.Row.DataItem,“流程培训%”).ToString();
}
其他的
{
lblProcessTraining.Visible=false;
}
var columnProduction=“Production%”;
var checkProduction=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(columnProduction,StringComparison.InvariantCultureIgnoreCase));
如果(检查生产)
{
//可用财产
lblProduction.Text=(DataBinder.Eval(e.Row.DataItem,“Production%”);
}
其他的
{
lblProduction.Visible=false;
}
var columnSystemDownTime=“系统停机%”;
var checkSystemDownTime=dataRow.Row.Table.Columns.Cast().Any(x=>x.ColumnName.Equals(columnSystemDownTime,StringComparison.InvariantCultureIgnoreCase));
如果(检查系统停机时间)
{
//可用财产
lblSystemDowntime.Text=(DataBinder.Eval(e.Row.DataItem,“系统宕机%”).ToString();
}
其他的
{
lblSystemDowntime.Visible=false;
}
var columnGrandTotal=“总计%”;
var checkGrandTotal=dataRow.Row.Tab
if Me.RoleId= True then
'Write code to Display
else
'Write code to Hide
end if
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label lblRoleNo = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblRoleId");
            System.Web.UI.WebControls.Label lblSupervisorName = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblSupervisorName");
            System.Web.UI.WebControls.Label lblUserECode = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblUserECode");
            System.Web.UI.WebControls.Label lblUserName = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblUserName");
            System.Web.UI.WebControls.Label lblDesignation = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblDesignation");
            System.Web.UI.WebControls.Label lblLDTraining = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblLDTraining");
            System.Web.UI.WebControls.Label lblNonProduction = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblNonProduction");
            System.Web.UI.WebControls.Label lblProcessSupport = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblProcessSupport");
            System.Web.UI.WebControls.Label lblProcessTraining = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblProcessTraining");
            System.Web.UI.WebControls.Label lblProduction = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblProduction");
            System.Web.UI.WebControls.Label lblSystemDowntime = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblSystemDowntime");
            System.Web.UI.WebControls.Label lblGrandTotal = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblGrandTotal");

            //Checking weather Columns exist in the Pivot or not

            var dataRow = (DataRowView)e.Row.DataItem;
            var columnNameToCheck = "L & D Training%";
            var checkTraining = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheck, StringComparison.InvariantCultureIgnoreCase));
            if (checkTraining)
            {
                // Property available
                lblLDTraining.Text = (DataBinder.Eval(e.Row.DataItem, "L & D Training%")).ToString();
            }
            else
            {
                lblLDTraining.Visible = false;
            }


            var columnNonProduction = "Non Production%";
            var checkNonProduction = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNonProduction, StringComparison.InvariantCultureIgnoreCase));
            if (checkNonProduction)
            {
                // Property available
                lblNonProduction.Text = (DataBinder.Eval(e.Row.DataItem, "Non Production%")).ToString();
            }
            else
            {
                lblNonProduction.Visible = false;
            }

            var columnProcessSupport = "Process Support%";
            var checkProcessSupport = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnProcessSupport, StringComparison.InvariantCultureIgnoreCase));
            if (checkProcessSupport)
            {
                // Property available
                lblProcessSupport.Text = (DataBinder.Eval(e.Row.DataItem, "Process Support%")).ToString();
            }
            else
            {
                lblProcessSupport.Visible = false;
            }

            var columnProcessTraining = "Process Training%";
            var checkProcesstraining = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnProcessTraining, StringComparison.InvariantCultureIgnoreCase));
            if (checkProcesstraining)
            {
                // Property available
                lblProcessTraining.Text = (DataBinder.Eval(e.Row.DataItem, "Process Training%")).ToString();
            }
            else
            {
                lblProcessTraining.Visible = false;
            }

            var columnProduction = "Production%";
            var checkProduction = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnProduction, StringComparison.InvariantCultureIgnoreCase));
            if (checkProduction)
            {
                // Property available
                lblProduction.Text = (DataBinder.Eval(e.Row.DataItem, "Production%")).ToString();
            }
            else
            {
                lblProduction.Visible = false;
            }


            var columnSystemDownTime = "System Downtime%";
            var checkSystemDownTime = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnSystemDownTime, StringComparison.InvariantCultureIgnoreCase));
            if (checkSystemDownTime)
            {
                // Property available
                lblSystemDowntime.Text = (DataBinder.Eval(e.Row.DataItem, "System Downtime%")).ToString();
            }
            else
            {
                lblSystemDowntime.Visible = false;
            }

            var columnGrandTotal = "Grand Total%";
            var checkGrandTotal = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnGrandTotal, StringComparison.InvariantCultureIgnoreCase));
            if (checkGrandTotal)
            {
                // Property available
                lblGrandTotal.Text = (DataBinder.Eval(e.Row.DataItem, "Grand Total%")).ToString();
            }
            else
            {
                lblGrandTotal.Visible = false;
            }



            var columnNameToCheckRoleID = "RoleId";
            var checkRoleID = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheckRoleID, StringComparison.InvariantCultureIgnoreCase));
            if (checkRoleID)
            {
                // Property available
                lblRoleNo.Text = (DataBinder.Eval(e.Row.DataItem, "RoleId")).ToString();
            }
            else
            {
                lblRoleNo.Visible = false;
            }

            var columnNameToCheckSupervisorName = "SupervisorName";
            var checkSupervisorName = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheckSupervisorName, StringComparison.InvariantCultureIgnoreCase));
            if (checkSupervisorName)
            {
                // Property available
                lblSupervisorName.Text = (DataBinder.Eval(e.Row.DataItem, "SupervisorName")).ToString();
            }
            else
            {
                lblSupervisorName.Visible = false;
            }


            var columnNameToCheckUserECode = "UserECode";
            var checkUserECode = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheckUserECode, StringComparison.InvariantCultureIgnoreCase));
            if (checkUserECode)
            {
                // Property available
                lblUserECode.Text = (DataBinder.Eval(e.Row.DataItem, "UserECode")).ToString();
            }
            else
            {
                lblUserECode.Visible = false;
            }

            var columnNameToCheckUserName = "UserName";
            var checkUserName = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheckUserName, StringComparison.InvariantCultureIgnoreCase));
            if (checkUserName)
            {
                // Property available
                lblUserName.Text = (DataBinder.Eval(e.Row.DataItem, "UserName")).ToString();
            }
            else
            {
                lblUserName.Visible = false;
            }

            var columnNameToCheckDesignation = "Designation";
            var checkDesignation = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(columnNameToCheckDesignation, StringComparison.InvariantCultureIgnoreCase));
            if (checkDesignation)
            {
                // Property available
                lblDesignation.Text = (DataBinder.Eval(e.Row.DataItem, "Designation")).ToString();
            }
            else
            {
                lblDesignation.Visible = false;
            }


            //Changing color of the Pivot Data

            if (Convert.ToInt32(lblRoleNo.Text.ToString()) == 2)
            {
                e.Row.BackColor = System.Drawing.Color.GreenYellow;
            }
            if (Convert.ToInt32(lblRoleNo.Text.ToString()) == 3)
            {
                e.Row.BackColor = System.Drawing.Color.Cyan;
            }
            if (Convert.ToInt32(lblRoleNo.Text.ToString()) == 4)
            {
                e.Row.BackColor = System.Drawing.Color.Orange;
            }
            if (Convert.ToInt32(lblRoleNo.Text.ToString()) == 5)
            {
                e.Row.BackColor = System.Drawing.Color.Pink;
            }






        }