[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP:DataGrid and how to bind to a BoundColumn

Posted on 2016-11-09
8
Medium Priority
?
81 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 37

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 37

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 37

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 37

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
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

864 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