Solved

ASP:DataGrid and how to bind to a BoundColumn

Posted on 2016-11-09
8
50 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 29

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 29

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 29

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 29

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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