【问题标题】:data Grid Data bound field header text change in c#c#中的数据网格数据绑定字段标题文本更改
【发布时间】:2017-11-02 14:48:06
【问题描述】:

我有一个有 BoundColumn 的数据网格,我正在尝试更改页面加载时的标题文本。我试过这个。

  <asp:datagrid id="dgdata" runat="server" Width="658px" CellPadding="2" PageSize="2" DataKeyField="Name"
                AutoGenerateColumns="False" ShowFooter="True" BorderColor="AliceBlue" OnItemDataBound="dgTranscript_ItemDataBound">
          <Columns>
                    <asp:BoundColumn DataField="Name" HeaderText="" ItemStyle-VerticalAlign="Top"></asp:BoundColumn>
</Columns>
        </asp:datagrid>

C#

 dgdata.Columns[1].Visible = true;
            dgdata.Columns[1].HeaderText = lblAverage.Text

我想将文本设置为该标签内的文本,但如果我说没有标签,它不会让我工作

 dgdata.Columns[1].Visible = true;
            dgdata.Columns[1].HeaderText = "Some Text";

绑定数据

 DataSet ds;
        DataRow drClient = null;
        dgdata.Columns[1].HeaderText = lblAverage.Text; // Here before the Daatabind I set the text to be that label 
        DataConn.WebExecute(out ds); 
        DataConn.Bind(dgTranscript, ds);// This binds the data to the datagrid

它将文本显示为标题,但是当我尝试插入任何字符串或标签文本时,它会拒绝整个标题消失 提前致谢。最好的问候

【问题讨论】:

    标签: c# asp.net datagrid boundfield headertext


    【解决方案1】:

    如果在数据网格上调用DataBind 后设置了lblAverage 的值,则标题将保持为空。

    这行得通

    lblAverage.Text = "Some Text";
    dgdata.Columns[0].HeaderText = lblAverage.Text;
    
    dgdata.DataSource = mySource;
    dgdata.DataBind();
    

    虽然不会

    lblAverage.Text = "Some Text";
    
    dgdata.DataSource = mySource;
    dgdata.DataBind();
    
    dgdata.Columns[0].HeaderText = lblAverage.Text;
    

    【讨论】:

    • 好吧,那没用。让我提供有关如何将其绑定到数据以及在何处设置 headertext 的问题的更多信息
    【解决方案2】:

    你可以试试这个。

     <asp:Label ID="lblAverage"  runat="server" Text="Header Value"></asp:Label>
            <asp:DataGrid ID="dgdata" runat="server" Width="658px" CellPadding="2" PageSize="2" DataKeyField="Name"
                AutoGenerateColumns="False" ShowFooter="true" ShowHeader="true" BorderColor="AliceBlue">
                <Columns>
                  <asp:TemplateColumn>
                        <HeaderTemplate>
                            <asp:Label ID="lblheader" runat="server" Text='<%# lblAverage.Text %>'></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblvalue" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
    

    【讨论】:

    • 不,先生,我只想设置位于顶部的那一列的标题,而不是添加另一行
    猜你喜欢
    • 2010-12-12
    • 2012-07-04
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多