473,749 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Populate DataGrid Using SqlDataReader

A SqlDataReader is populated with the records from a SQL Server 2005 DB
table. The records retrieved depends upon 2 conditions (the conditions
depend on what a user selects in an ASPX page). If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.

Moreover the 2 tables do not have the same no. of columns - even the
column names are different. This means that the DataGrid has to be
filled dynamically. Had this not been the case, then using the
DataField property of the DataGrid, I could have populated the DataGrid
using

<asp:DataGrid ID="dgAddress" runat="server">
<Columns>
<asp:BoundColum n DataField="Addr ess" HeaderText="ADD RESS"/>
<asp:BoundColum n DataField="City " HeaderText="CIT Y"/>
<asp:BoundColum n DataField="Stat e" HeaderText="STA TE"/>
<asp:BoundColum n DataField="Coun try" HeaderText="COU NTRY"/>
<asp:BoundColum n DataField="Zip" HeaderText="ZIP "/>
</Columns>
</asp:DataGrid>

But since I need to populate the DataGrid dynamically, the above
approach won't be feasible. So I tried to do it in the Page_Load sub
but I don't find any property named 'DataField' when I browsed through
the different properties of the DataGrid.

So how do I populate the DataGrid dynamically?

Oct 23 '06 #1
3 4368

If you want to use dynamic columns for your datasource, then you should
dynamically create your DataGrid.

Do a google search for

datagrid "new BoundColumn"
Or redesign/rethink your approach.

//Quote
If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.
//End Quote
That sounds very hacky........
Here is some code I pulled from a hit (using that google search)

public void TestHyperLinkCo lumn()
{
// First add a simple bound column
BoundColumn nameColumn = new BoundColumn();
nameColumn.Data Field = "ProductNam e";
nameColumn.Data FormatString = "{0}";
nameColumn.Head erText = "Product";

// Now add the HyperLink column
HyperLinkColumn linkColumn = new HyperLinkColumn ();
linkColumn.Data TextField = "ProductNam e";
linkColumn.Data TextFormatStrin g = "{0} Details";
linkColumn.Data NavigateUrlFiel d = "ProductID" ;
linkColumn.Data NavigateUrlForm atString =
"/MyApp/ProductDetails. aspx={0}";
linkColumn.Head erText = "Details";

// Add the link in a BoundColumn
// where the text can be the same for all rows
BoundColumn blinkColumn = new BoundColumn();
blinkColumn.Dat aField = "ProductID" ;
blinkColumn.Dat aFormatString =
"<a href='/MyApp/ProductDetails. aspx={0}'>Detai ls</a>";
blinkColumn.Hea derText = "Details";

DataGrid1.Colum ns.Add(nameColu mn);
DataGrid1.Colum ns.Add(linkColu mn);
DataGrid1.Colum ns.Add(blinkCol umn);
DataGrid1.AutoG enerateColumns = false;

DataTable dt = GetNorthwindPro ductTable();
DataGrid1.DataS ource = dt;
DataGrid1.DataB ind();

}


<rn**@rediffmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
A SqlDataReader is populated with the records from a SQL Server 2005 DB
table. The records retrieved depends upon 2 conditions (the conditions
depend on what a user selects in an ASPX page). If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.

Moreover the 2 tables do not have the same no. of columns - even the
column names are different. This means that the DataGrid has to be
filled dynamically. Had this not been the case, then using the
DataField property of the DataGrid, I could have populated the DataGrid
using

<asp:DataGrid ID="dgAddress" runat="server">
<Columns>
<asp:BoundColum n DataField="Addr ess" HeaderText="ADD RESS"/>
<asp:BoundColum n DataField="City " HeaderText="CIT Y"/>
<asp:BoundColum n DataField="Stat e" HeaderText="STA TE"/>
<asp:BoundColum n DataField="Coun try" HeaderText="COU NTRY"/>
<asp:BoundColum n DataField="Zip" HeaderText="ZIP "/>
</Columns>
</asp:DataGrid>

But since I need to populate the DataGrid dynamically, the above
approach won't be feasible. So I tried to do it in the Page_Load sub
but I don't find any property named 'DataField' when I browsed through
the different properties of the DataGrid.

So how do I populate the DataGrid dynamically?

Oct 23 '06 #2
Sloan, this is what I tried:

Sub Page_Load(....)
Dim BillAdd As DataGrid

BillAdd = New DataGrid
BillAdd.ID = "dgBillAdd"
i = 2

BillAdd.DataSou rce = sqlReader
While (sqlReader.Read )
BillAdd.Columns .Add(CreateBoun dColumn(sqlRead er.GetName(i)))
i = i + 1
End While

BillAdd.DataBin d()
phlAddress.Cont rols.Add(BillAd d)
End Sub

Function CreateBoundColu mn(ByVal ColumnName As String) As BoundColumn
Dim bColumn As BoundColumn
bColumn = New BoundColumn

bColumn.DataFie ld = ColumnName
bColumn.HeaderT ext = ColumnName

Return bColumn
End Function

But the above code just displays the HeaderText of the column whose
index is 2 in the DataGrid & the HeaderText of the rest of the columns
don't get displayed in the DataGrid. Moreover, none of the records get
displayed in the DataGrid.

What am I missing here?
sloan wrote:
If you want to use dynamic columns for your datasource, then you should
dynamically create your DataGrid.

Do a google search for

datagrid "new BoundColumn"
Or redesign/rethink your approach.

//Quote
If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.
//End Quote
That sounds very hacky........
Here is some code I pulled from a hit (using that google search)

public void TestHyperLinkCo lumn()
{
// First add a simple bound column
BoundColumn nameColumn = new BoundColumn();
nameColumn.Data Field = "ProductNam e";
nameColumn.Data FormatString = "{0}";
nameColumn.Head erText = "Product";

// Now add the HyperLink column
HyperLinkColumn linkColumn = new HyperLinkColumn ();
linkColumn.Data TextField = "ProductNam e";
linkColumn.Data TextFormatStrin g = "{0} Details";
linkColumn.Data NavigateUrlFiel d = "ProductID" ;
linkColumn.Data NavigateUrlForm atString =
"/MyApp/ProductDetails. aspx={0}";
linkColumn.Head erText = "Details";

// Add the link in a BoundColumn
// where the text can be the same for all rows
BoundColumn blinkColumn = new BoundColumn();
blinkColumn.Dat aField = "ProductID" ;
blinkColumn.Dat aFormatString =
"<a href='/MyApp/ProductDetails. aspx={0}'>Detai ls</a>";
blinkColumn.Hea derText = "Details";

DataGrid1.Colum ns.Add(nameColu mn);
DataGrid1.Colum ns.Add(linkColu mn);
DataGrid1.Colum ns.Add(blinkCol umn);
DataGrid1.AutoG enerateColumns = false;

DataTable dt = GetNorthwindPro ductTable();
DataGrid1.DataS ource = dt;
DataGrid1.DataB ind();

}


<rn**@rediffmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
A SqlDataReader is populated with the records from a SQL Server 2005 DB
table. The records retrieved depends upon 2 conditions (the conditions
depend on what a user selects in an ASPX page). If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.

Moreover the 2 tables do not have the same no. of columns - even the
column names are different. This means that the DataGrid has to be
filled dynamically. Had this not been the case, then using the
DataField property of the DataGrid, I could have populated the DataGrid
using

<asp:DataGrid ID="dgAddress" runat="server">
<Columns>
<asp:BoundColum n DataField="Addr ess" HeaderText="ADD RESS"/>
<asp:BoundColum n DataField="City " HeaderText="CIT Y"/>
<asp:BoundColum n DataField="Stat e" HeaderText="STA TE"/>
<asp:BoundColum n DataField="Coun try" HeaderText="COU NTRY"/>
<asp:BoundColum n DataField="Zip" HeaderText="ZIP "/>
</Columns>
</asp:DataGrid>

But since I need to populate the DataGrid dynamically, the above
approach won't be feasible. So I tried to do it in the Page_Load sub
but I don't find any property named 'DataField' when I browsed through
the different properties of the DataGrid.

So how do I populate the DataGrid dynamically?
Oct 23 '06 #3

You're trying to reuse the sqlReader. You only get ONE loop through on the
datareader object. You can't reuse it.

You're looping on it once to create the columns.
You're ~trying to reloop on it, when it gets bound to the grid. But you
can't reloop on it, thus you don't see any data-records/rows.

...

<rn**@rediffmai l.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
Sloan, this is what I tried:

Sub Page_Load(....)
Dim BillAdd As DataGrid

BillAdd = New DataGrid
BillAdd.ID = "dgBillAdd"
i = 2

BillAdd.DataSou rce = sqlReader
While (sqlReader.Read )
BillAdd.Columns .Add(CreateBoun dColumn(sqlRead er.GetName(i)))
i = i + 1
End While

BillAdd.DataBin d()
phlAddress.Cont rols.Add(BillAd d)
End Sub

Function CreateBoundColu mn(ByVal ColumnName As String) As BoundColumn
Dim bColumn As BoundColumn
bColumn = New BoundColumn

bColumn.DataFie ld = ColumnName
bColumn.HeaderT ext = ColumnName

Return bColumn
End Function

But the above code just displays the HeaderText of the column whose
index is 2 in the DataGrid & the HeaderText of the rest of the columns
don't get displayed in the DataGrid. Moreover, none of the records get
displayed in the DataGrid.

What am I missing here?
sloan wrote:
If you want to use dynamic columns for your datasource, then you should
dynamically create your DataGrid.

Do a google search for

datagrid "new BoundColumn"
Or redesign/rethink your approach.

//Quote
If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.
//End Quote
That sounds very hacky........
Here is some code I pulled from a hit (using that google search)

public void TestHyperLinkCo lumn()
{
// First add a simple bound column
BoundColumn nameColumn = new BoundColumn();
nameColumn.Data Field = "ProductNam e";
nameColumn.Data FormatString = "{0}";
nameColumn.Head erText = "Product";

// Now add the HyperLink column
HyperLinkColumn linkColumn = new HyperLinkColumn ();
linkColumn.Data TextField = "ProductNam e";
linkColumn.Data TextFormatStrin g = "{0} Details";
linkColumn.Data NavigateUrlFiel d = "ProductID" ;
linkColumn.Data NavigateUrlForm atString =
"/MyApp/ProductDetails. aspx={0}";
linkColumn.Head erText = "Details";

// Add the link in a BoundColumn
// where the text can be the same for all rows
BoundColumn blinkColumn = new BoundColumn();
blinkColumn.Dat aField = "ProductID" ;
blinkColumn.Dat aFormatString =
"<a href='/MyApp/ProductDetails. aspx={0}'>Detai ls</a>";
blinkColumn.Hea derText = "Details";

DataGrid1.Colum ns.Add(nameColu mn);
DataGrid1.Colum ns.Add(linkColu mn);
DataGrid1.Colum ns.Add(blinkCol umn);
DataGrid1.AutoG enerateColumns = false;

DataTable dt = GetNorthwindPro ductTable();
DataGrid1.DataS ource = dt;
DataGrid1.DataB ind();

}


<rn**@rediffmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
A SqlDataReader is populated with the records from a SQL Server 2005
DB
table. The records retrieved depends upon 2 conditions (the conditions
depend on what a user selects in an ASPX page). If condition1 is true,
then the SqlDataReader will be populated with the records existing in
table1 & will return 0 to the calling function but if condition2 is
true, then the SqlDataReader will be populated with the records from
another table named table2 & will return 1 to the calling function.
>
Moreover the 2 tables do not have the same no. of columns - even the
column names are different. This means that the DataGrid has to be
filled dynamically. Had this not been the case, then using the
DataField property of the DataGrid, I could have populated the
DataGrid
using
>
<asp:DataGrid ID="dgAddress" runat="server">
<Columns>
<asp:BoundColum n DataField="Addr ess" HeaderText="ADD RESS"/>
<asp:BoundColum n DataField="City " HeaderText="CIT Y"/>
<asp:BoundColum n DataField="Stat e" HeaderText="STA TE"/>
<asp:BoundColum n DataField="Coun try" HeaderText="COU NTRY"/>
<asp:BoundColum n DataField="Zip" HeaderText="ZIP "/>
</Columns>
</asp:DataGrid>
>
But since I need to populate the DataGrid dynamically, the above
approach won't be feasible. So I tried to do it in the Page_Load sub
but I don't find any property named 'DataField' when I browsed through
the different properties of the DataGrid.
>
So how do I populate the DataGrid dynamically?
>

Oct 24 '06 #4

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

Similar topics

1
18250
by: Brian | last post by:
I have a dataset containing 2 tables. I need to fill a datagrid using data from both of these. If I could create a SQL Statement to fill the datagrid, it would look like this: SELECT fields.fieldname, fieldvalues.value FROM fields, fieldvalues I am having trouble finding documentation explaining how to populate
0
1187
by: Billy | last post by:
I have a web page with a control (Control#A) on it. The control (#A) has a control (Control#B) on it which displays an exception message when set and displayed. I am creating a simple datagrid dynamically with delegates and assigning it to a placeholder on control #A. If, within a delegate of the dynamically built datagrid, I set a property on control #B, it sets fine but fails to reflect this on the displayed page. There is no...
1
1945
by: GP | last post by:
I have created a datagrid that is creating columns dynamically at runtime using the template columns.Edit ,add & delete works fine. The only problem I face is with sorting,I am setting the sorting expression to the column name and checked the allow sorting checkbox to true. The code never reaches the DataGrid1_SortCommand,But I do see the underlines in the header text which looks it will sorting.Any help is greatly appreiciated. Thanks GP
4
6200
by: dyw55a | last post by:
Donna Mar 15, 10:11 am show options Newsgroups: microsoft.public.dotnet.framework.adonet From: "Donna" <dyw...@yahoo.com> - Find messages by this author Date: 15 Mar 2005 10:11:56 -0800 Local: Tues, Mar 15 2005 10:11 am Subject: VB.NET dynamically create datagrid and set its datasource Reply | Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse
3
6898
by: Yi Chen | last post by:
We have a drop down list on a PHP page, with several product names, and when people click one item, we will refresh the same page with the product name as parameter, and in turn we want to include a HTML file into the content area of the same page. I know it is recommended to put everything into database, but we want the web site to be very "portable", so the drop-downlist and the content should both in text files. Let's say the...
0
382
by: rn5a | last post by:
A SqlDataReader is populated with the records from a SQL Server 2005 DB table. The records retrieved depends upon 2 conditions (the conditions depend on what a user selects in an ASPX page). If condition1 is true, then the SqlDataReader will be populated with the records existing in table1 & will return 0 to the calling function but if condition2 is true, then the SqlDataReader will be populated with the records from another table named...
0
1293
by: chandutp | last post by:
hi i am new to this forum can i populate datagrid items of asp page to another asp page having textfields. in this datagrid userId is the index key by which it referenced.and i am using edit button in that grid when i click on edit button it has to populate the corresponding userid items to the another asppage containing textfields. i had tried this by querystring but it is not done.please help me . ...
1
1282
by: teddarr | last post by:
I have a TList that returns several items in C#. I want to pull 3 entities from this tlist to assign to variables. 2 of the 3 will be displayed in a datagrid. How do I assign these values to the variables and then populate my datagrid? Here's what I have so far... private void btnDelPointSearch_Click(object sender, EventArgs e) { m_delPoint = textBoxDeliveryPoint.Text; try {
1
2803
by: Soulless | last post by:
Hi, I'm such a nood it isn't funny and apologize for this question in advance. I have a datagrid on my form and wish to populate it from a Sybase source using a dynamic select. so, basically I can create a select statement in a string and retrieve it using ExecuteScalar to retrieve. I am not sure though how I can
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.