Reputation: 1
I have a problem to show data in gridview in different columns but data is displaying in a single column.
design
<asp:GridView ID="GridView1" runat="server" class="international_news" AutoGenerateColumns="False" DataKeyNames="news_id" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="news_id"/>
<asp:BoundField DataField="news_title"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ReadMore" CommandArgument='<%#Eval("news_id")%>'>
Read More
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind
if (!this.IsPostBack)
{
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("news_id", typeof(string)),
new DataColumn("news_title", typeof(string))});
while (dr.Read())
{
if (dr[1].ToString() != "yes")
{
dt.Rows.Add(dr[0].ToString());
dt.Rows.Add(dr[1].ToString());
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Upvotes: 0
Views: 81
Reputation: 180
I am not positive what you are exactly running into - are you saying that the gridview control, when displaying on the HTML page, is only showing one column? If so, can you tell us what is showing in the column?
Also, I see a problem in your "code behind" - you should not be setting and binding the gridview inside your while loop. Move it after the loop, and see if that makes a difference in what you are seeing.
Okay, so you are seeing two columns at this point - not one (from what I can tell). At this point, the concern would be the linkbutton not appearing, correct? Do you see ANY HTML on the page, if you look at the page source, that might be the linkbutton? If so, can you post what you see?
Also, please let us know if anything changes when you bind the gridview outside of the while loop.
It sounds like you are now seeing all three columns at this point, based on your most recent comment. Are there further issues? Are you expecting to see something different? Can you tell us what you are expecting to see versus what you are actually seeing?
Upvotes: 1