Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic datagrid column headings 1

Status
Not open for further replies.

aaronjonmartin

Technical User
Jul 9, 2002
475
GB
Hi hope I can explain this well enough.

I have a datagrid which displays the results of a query (i.e. they are bound to this datagrid). And that works fine. However I have another query which returns 1 column and 5 rows. I want the text in each of the 5 rows to be the heading of each of the five columns of the datagrid.

Is this possible?

Im guessing you cant bind two datasources to one datagrid?

If it is possible where can I see some code (im using VB) which shows how i could do this?

Thanks for any help in advance.

Aaron

"It's so much easier to suggest solutions when you don't know too much about the problem."
Malcolm Forbes (1919 - 1990)
 
Why don't you make an ArrayList of the column names from your first grid aand then bind that to your 2nd?
 
thanks for the reply. Im not sure I follow you though. Are you suggesting I should use 2 datagrids?

"It's so much easier to suggest solutions when you don't know too much about the problem."
Malcolm Forbes (1919 - 1990)
 
Oh wait I read your question wrong.

Here is what you do. Give your DataGrid an OnDataBinding event and handle it like so:

Code:
    public void dgCarriers_OnDataBinding(Object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {

            e.Item.Cells[2].Text = DatabaseFunctions.GetRebateHeading(Convert.ToInt32(Session["SiteID"])) + " up to";
            e.Item.Cells[2].Font.Bold = true;

        }



    }
[\code]

This is where you do your 2nd query, then loop through all e.Item.Cells[x].Text and replace the HEadings.

[code]

DataSet ds = SomeFunctionToFilMyDatasetWithColumnHeadings;

for(int x= 0; x < ds.Tables[0].Rows.Count; x++)
{

    e.Item.Cells[x].Text = Convert.ToString(ds.Tables[0].Row[x]["headingfieldname"]);


}

or

Code:
int x = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    e.Items.Cells[x].Text = Convert.ToString(dr["headingfieldname"]);

    x++;

}
 
>>I want the text in each of the 5 rows to be the heading of each of the five columns of the datagrid.


what does that mean? do you want 1 heading per row?

Known is handfull, Unknown is worldfull
 
sorry if i wasnt very clear.

For example the five rows returned by the second query could be:

Liverpool V Man United
Arsenal V Spurs
Wigan V Everton
Newcastle V Blackburn
Charlton V Middlesborough

I want each of these to be the heading of a column in the datagrid. So Liverpool V Man United would be the heading of the first column, and so on. Hope thats a bit clearer.

thanks for the code tperri I will try that now.

&quot;It's so much easier to suggest solutions when you don't know too much about the problem.&quot;
Malcolm Forbes (1919 - 1990)
 
tperri your suggestion worked perfectly. Thankyou very much.

&quot;It's so much easier to suggest solutions when you don't know too much about the problem.&quot;
Malcolm Forbes (1919 - 1990)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top