Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

ASP:DataGrid and how to bind to a BoundColumn

I will be required to start working with ASP:DataGrid controls.(Not GridView). I want to create a very basic DataGrid with one or two columns of Data using data bound columns. Below I have create a basic ASP:DataGrid. I bind the data source to my Grid using
some simple data set data I created in C#. It works great however I am gettng the same column duplicated twice? How can  change
the code so that the data source is one column which maps to by bound data column below in my asp markup. DataField ="Name"?


<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700px" EnableViewState="False">
<Columns>
<asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Name">
   <HeaderStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</asp:Content>

---------------------------
namespace WebApplication1
{
  public partial class _Default : Page
  {
     DataSet ds = new DataSet();
     DataTable dt = new DataTable();
     dt.Columns.Add("Name", Type.GetType("System.String"));
     dt.Rows.Add(new object[] {"James Bawn"});
     ds.Tables.Add(dt);
     MyDataGrid.DataSource=dt;
     MyDataGrid.DataBind();
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Try this

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
 <asp :GridView ID="GridViewPawan" runat="server" AutoGenerateColumns="false">
  <columns>
   <asp :BoundField HeaderText="Name" DataField="Name" />   
 </columns>
 </asp:GridView>
</asp:Content>

DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add("James Bawn");
GridViewPawan.DataSource=dt;
GridViewPawan.DataBind();

Open in new window

Avatar of brgdotnet

ASKER

Pawan I am mot using a GridView control sir. I am using an asp.net DataGrid control.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AutoGenerate columns is not in my markup. It looks like you posted in some other code Pawan, and not the code I posted.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Pawan for the help. I won't be able to close this question out until tomorrow. I appreciate your help.
Closing question is not a problem. Your problem should be fixed. :)

Regards,
Pawan
Thank you Pawan, you are truly great!!