Solved

ASP:DataGrid and how to bind to a BoundColumn

Posted on 2016-11-09
8
30 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 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Accepted Solution

by:
Pawan Kumar Khowal 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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 18

Expert Comment

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now