Jul 11th, 2011, 02:18 AM
#1
[RESOLVED] Gridview bound column issue
Hi All,
I am trying to create a bound column and bind the data with the below piece of code . And the column has two files Name and Fee but the code binds only the Fees column and Name column become empty.. Any idea ?
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// BuildColumnsDynamically is called in InitializeComponent method
BuildColumnsDynamically();
}
}
private void BuildColumnsDynamically()
{
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
BoundField feecolumn = new BoundField();
nameColumn.DataField = "Fee";
nameColumn.HeaderText = "Fees";
GridView1.Columns.Insert(0,nameColumn);
GridView1.Columns.Insert(1,feecolumn);
FillGridView();
}
private void FillGridView()
{
int[] ids = { 12, 13, 14, 15, 16 };
string[] names = { "Alice", "James", "Peter", "Simon", "David" };
int[] fee = { 2299, 5123, 7564, 9595, 1600 };
decimal[] prices = { 12.99m, 122.23m, 25.64m, 66.85m, 1.60m };
decimal[] discounts = { 0.2m, 0.194m, 0.4564m, 0.209m, 0.310m };
decimal[] differences = { -12m, 19.4m, -45.64m, 200.9m, 41.60m };
string[] dates = { "04-12-2010", "07-23-2010", "07-14-2009", "12-12-2010", "11-03-2019" };
bool[] onSale = { true, false, true, true, false };
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(System.String));
table.Columns.Add("Fee", typeof(System.Decimal));
for (int i = 0; i < 5; i++)
{
DataRow row = table.NewRow();
row["Name"] = names[i];
row["Fee"] = fee[i];
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
private void InitializeComponent()
{
this.Init += new System.EventHandler(this._Default_Init);
this.Load += new System.EventHandler(this.Page_Load);
}
private void _Default_Init(object sender, EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
}
Design Part
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
th
{
cursor:pointer;
background-color:#dadada;
color:Black;
font-weight:bold;
text-align:left;
}
th.headerSortUp
{
background-image: url(images/asc.gif);
background-position: right center;
background-repeat:no-repeat;
}
th.headerSortDown
{
background-image: url(images/desc.gif);
background-position: right center;
background-repeat:no-repeat;
}
td
{
border-bottom: solid 1px #dadada;
}
</style>
<script src="scripts/jquery-1.4.3.min.js" type="text/javascript"></script>
<script src="scripts/jquery.tablesorter.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#GridView1").tablesorter();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
>
</asp:GridView>
</form>
</body>
</html>
Attached Files
Last edited by danasegarane; Jul 11th, 2011 at 03:33 AM .
Reason: Attachment added
Please mark you thread resolved using the Thread Tools as shown
Jul 11th, 2011, 05:17 AM
#2
Re: Gridview bound column issue
It all looks ok to me so it may be something simple that decieves the eye. Try setting the grid to autogenerate columns = true and call FillGridView() from page load. If that's ok then you know the problems in BuildColumnsDynamically() or vice versa.
The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.
Jul 11th, 2011, 05:28 AM
#3
Re: Gridview bound column issue
I tried the auto generated columns =true and worked well. But My doubt why the other method is not working...
Please mark you thread resolved using the Thread Tools as shown
Jul 11th, 2011, 02:09 PM
#4
Re: Gridview bound column issue
Hello,
Looks like you have a copy and paste error
Change this:
Code:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
BoundField feeColumn = new BoundField();
nameColumn.DataField = "Fee";
nameColumn.HeaderText = "Fees";
To this:
Code:
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name";
BoundField feeColumn = new BoundField();
feeColumn.DataField = "Fee";
feeColumn.HeaderText = "Fees";
Gary
Jul 12th, 2011, 01:21 AM
#5
Re: Gridview bound column issue
Oh Sorry for that one
Please mark you thread resolved using the Thread Tools as shown
Jul 12th, 2011, 01:26 AM
#6
Re: [RESOLVED] Gridview bound column issue
Nothing to apologise for!! Everyone makes these type of mistakes and they are the hardest ones to find, especially if you have been looking at the code for a long time. That is why forums like this are great because you can get a lot of different sets of eyes looking at the code
Gary
Jul 12th, 2011, 06:10 AM
#7
Re: [RESOLVED] Gridview bound column issue
It took me 3 reads of Gary's before and after code to see the difference. It's coding mind games
The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.
Jul 12th, 2011, 06:14 AM
#8
Re: [RESOLVED] Gridview bound column issue
Originally Posted by
brin351
It took me 3 reads of Gary's before and after code to see the difference. It's coding mind games
Ha ha, I was the same when I was looking at it.
It wasn't until I started to comment things out that I saw what was going on, but it certainly wasn't immediately obvious. One of those Eureka moments
Gary
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width