Multiple Datagrids

Z

Z3Stealth

I am trying to create an app that will be able to grab data from many
different databases, and combine that data into one page. I do not know at
design time how many servers I will need to connect to, that information is
collected at runtime by reading a configuration file. What I want to do is
display the data from each server in a separate datagrid on my web form. I
tried to accomplish this by creating an array of DataGrid controls, and
assigning the data from each server to a different object in the array. I am
attempting to display the datagrid with the following code:

<%
for (int i = 0; i < NumServers; i++)
{
Response.Write("<asp:DataGrid ID=\"dgrData[");
Response.Write("i");
Response.Write("]\" HorizontalAlign=\"Center\" CellPadding=\"3\"
Runat=\"server\" /><br>");
}
%>

This approach does not seem to be working, as Response.Write writes the
<asp:DataGrid ...> tag, and doesn't actually process it to create the
DataGrid. Is there a way to display DataGrids when I don't know how many
there will be?
 
A

aa7im

You must add the DataGrid to the Control collection in the page's
Init() method somthing like:
DataGrid myDG = new DataGrid();
Page.Controls.Add(myDG);
 
Z

Z3Stealth

Thank you for your reply. I added the code you suggested, and am getting an
error when the page loads. Now I have the following code:

<body>
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DataGrid[] dgrData;
Page.Controls.Add(dgrData);
}
</script>

<asp:DataGrid ID="dgrData" HorizontalAlign="Center" CellPadding="3"
Runat="server" /><br>
</body>

The error I am getting is:
CS1502: The best overloaded method match for
'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some invalid
arguments
 
Z

Z3Stealth

When I comment out the Page.Controls.Add(dgrData), the page loads, but the
DataGrid is blank. Most likely because it is looking for a DataGrid, and I
gave it a Datagrid[] array. Is there a way to display a DataGrid[] object?
I would like to loop through the array and display each DataGrid in the array
one at a time.


Z3Stealth said:
Thank you for your reply. I added the code you suggested, and am getting an
error when the page loads. Now I have the following code:

<body>
<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DataGrid[] dgrData;
Page.Controls.Add(dgrData);
}
</script>

<asp:DataGrid ID="dgrData" HorizontalAlign="Center" CellPadding="3"
Runat="server" /><br>
</body>

The error I am getting is:
CS1502: The best overloaded method match for
'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some invalid
arguments

aa7im said:
You must add the DataGrid to the Control collection in the page's
Init() method somthing like:
DataGrid myDG = new DataGrid();
Page.Controls.Add(myDG);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top