0

I have a datagrid that has a BoundColumn there I am trying to change the header text on page load I so fa. I have tried this.

  <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

I want to set the text to be the text that's inside that label but its not letting me if i say without the label it works

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

Binding Data

 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

It shows that text as the header but when I try punting in any string or a label text it denies the whole header disappears Thanks in advance. Best Regards

2 Answers 2

2

If the value of lblAverage is set after calling DataBind on the datagrid then the header will remain empty.

This works

lblAverage.Text = "Some Text";
dgdata.Columns[0].HeaderText = lblAverage.Text;

dgdata.DataSource = mySource;
dgdata.DataBind();

While this will not

lblAverage.Text = "Some Text";

dgdata.DataSource = mySource;
dgdata.DataBind();

dgdata.Columns[0].HeaderText = lblAverage.Text;
Sign up to request clarification or add additional context in comments.

1 Comment

Ok yeah that didn't work. let me put in more info on the question how i am binding it to the data and where i set the headertext
0

You can try this.

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

1 Comment

Nope Sir I just want to set the header of that one Column that's on the top not add another row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.