Solved

ASP:DataGrid and how to bind to a BoundColumn

Posted on 2016-11-09
8
43 Views
Last Modified: 2016-11-14
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();
0
Comment
Question by:brgdotnet
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41881506
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

0
 
LVL 2

Author Comment

by:brgdotnet
ID: 41881540
Pawan I am mot using a GridView control sir. I am using an asp.net DataGrid control.
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41881553
ok, Try AutoGenerateColumns="false"  is the issue

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <ASP:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="false" Width="700px">
     <Columns>
       <asp:BoundColumn DataField="Name" HeaderText="Namex">    
       </asp:BoundColumn>
     </Columns>
    </asp:DataGrid>
</asp:Content>

protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add("James Bawn");
            MyDataGrid.DataSource = dt;
            MyDataGrid.DataBind();
        }

Open in new window


Output
-----------

EE
Hope it helps !
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:brgdotnet
ID: 41884400
AutoGenerate columns is not in my markup. It looks like you posted in some other code Pawan, and not the code I posted.
0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 500 total points
ID: 41884402
You have to add it.

If you DONT add below property then by default .NET engine takes this property as true and it will generate the columns for you. that's why you are getting duplicate columns. - One is by AutoGenerateColumns and other one by you Bound Column.


Hope it helps !!
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 41885822
Thanks Pawan for the help. I won't be able to close this question out until tomorrow. I appreciate your help.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41885832
Closing question is not a problem. Your problem should be fixed. :)

Regards,
Pawan
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 41887276
Thank you Pawan, you are truly great!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question