Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP:DataGrid and how to bind to a BoundColumn

Posted on 2016-11-09
8
Medium Priority
?
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 32

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 32

Accepted Solution

by:
Pawan Kumar earned 2000 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
Industry Leaders: 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 32

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 2000 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 32

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

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!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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