473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

datagrid from scratch/events problem

Ok, I think this is an easy one - I've just been staring at it too long.

I'm creating a datagrid in asp.net/c# from scratch, i.e. nothing at all in
the aspx page. I'm also creating all the columns programmatically
(templatecolumns). This part works fine. Next, I am adding ImageButtons to
the headers of the columns, which I am attaching events to and using to sort
the datagrid based on the given column. This works with one major problem -
the datagrid is loaded TWICE. I thought I knew the obvious explanation for
this (I understand postbacks, etc), but I'm obviously missing something.

I tried stepping back and using an example straight from Microsoft's KB. I
created a new asp.net solution using some sample code from the example, and
changed one of the columns in their example to a programmatically created
boundcolumn (it was previously in the aspx page). See code below. If you run
the code below, you'll see the page loads fine, and sorting on the columns
created in the aspx page works fine. However, the programatically created
column simply dissapears when you click it's sort button. What gives??
SortableDataGrid.aspx:

<%@ Page language="c#" Codebehind="SortableDataGrid.aspx.cs"
AutoEventWireup="false" Inherits="dev.SortableDataGrid" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SortableDataGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgProducts" style="Z-INDEX: 101; LEFT:
40px; POSITION: absolute; TOP: 32px"
runat="server">
<Columns>
<asp:BoundColumn DataField="UnitPrice"
SortExpression="UnitPrice" HeaderText="UnitPrice"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitsInStock"
SortExpression="UnitsInStock" HeaderText="UnitsInStock"></asp:BoundColumn>
</Columns>
</asp:DataGrid></form>
</body>
</HTML>

SortableDataGrid.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace dev
{
/// <summary>
/// Summary description for SortableDataGrid.
/// </summary>
public class SortableDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgProducts;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
BindData("ProductName");
}

private void AddColumns()
{
}

private void BindData(string orderBy)
{
dgProducts.AutoGenerateColumns = false;
dgProducts.AllowSorting = true;

BoundColumn bc1 = new BoundColumn();
bc1.DataField = "ProductName";
bc1.SortExpression = "ProductName";
bc1.HeaderText = "ProductName";
dgProducts.Columns.Add(bc1);

// Connect to the Database
SqlConnection myConnection = new
SqlConnection("server=127.0.0.1;database=Northwind ;uid=TestUser;pwd=TestPwd;");

// Retrieve the SQL query results and bind it to the Repeater
string SQL_QUERY = "SELECT ProductName, UnitPrice,
UnitsInStock " +
"FROM Products ORDER BY " + orderBy;
SqlCommand myCommand = new SqlCommand(SQL_QUERY,
myConnection);

myConnection.Open();
dgProducts.DataSource = myCommand.ExecuteReader();
dgProducts.DataBind();
myConnection.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgProducts.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.dgProducts_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgProducts_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEvent Args e)
{
BindData(e.SortExpression);
}
}
}


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.806 / Virus Database: 548 - Release Date: 12/5/2004
Nov 19 '05 #1
0 1089

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some...
2
by: andla | last post by:
Hi, How does events fire in a datagrid. I know about the problem if turning the viewstate off the events wil not fire properly even if I rebind the control in every postback. S then I started...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
1
by: Peter Rilling | last post by:
I have an interesting problem with a datagrid. It is the standard chicken-and-the-egg problem. I have this page with two datagrids. It essentially defines a parent-child relationship. The...
2
by: Luis Esteban Valencia Muñoz | last post by:
I have a datagrid that displays editable text fields (2 different price fields) and a checkbox in every row. It has a "SaveChanges" button at the bottom, which, when pressed, looks at every...
2
by: Jon | last post by:
My application needs to present data generated at run time in a datagrid that can be sorted by clicking on columns, and where rows can be selected and events generated. Do I need to set up a...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
4
by: Jeff User | last post by:
Hi I tryed to solve this problem over in the framework.asp group, but still am having trouble. Hope someone here can help. using .net 1.1, VS 2003 and C# I have an asp.DataGrid control with a...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.