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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine headertext on itemdatabound of datagrid cell

Status
Not open for further replies.

Lbob

Programmer
May 23, 2003
157
GB
Hi
I'm dynamically adding buttons in datagrid cells on the itemdatabound function. I'm also trying to determine which column I'm in and pull out the headertext of that column.
This is what I've got so far

Private Sub dgData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgData.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.SelectedItem Then


If e.Item.Cells(i).Text = "Book" Then

btn = New Button
btn.Text = "Book"
btn.ID = "Btn-" & i '& Add the columnheader
btn.CssClass = "btn"
AddHandler btn.Click, AddressOf btn_Click
e.Item.Cells(i).Controls.Add(btn)
end if


end if
any help would be greatly appreciated
lbob
 
what are you trying to do? If the current column name = "Book" then add a button?
 
For a datagrid itemdatabound, it processes each row sequentially.

Your header row will be rendered first, at which point you can record the header text, then the rows below.

So the current way that i know of doing it is capturing the header text into an array and then referencing it later when it is an Item or AlternatingItem.

This is in c#, hopefully you can get a conversion to vb.
and i hope this example will help more than hurt!
Code:
string[] headertext = new string[50];
//the string initialized with a max value
//i went with 50 to handle the most columns i would have

public void dgReport_IDB(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        int total = e.Item.Controls.Count;
        for (int i = 0; i < total; i++)
        {
            headertext[i] = e.Item.Cells[i].Text;
        }
    }
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        e.Item.Cells[0].HorizontalAlign = HorizontalAlign.Left;
        e.Item.Cells[0].Wrap = false;
        int total = e.Item.Controls.Count;
        for (int i = 1; i < total; i++)
        {
            e.Item.Cells[i].Width = 65;
            if (e.Item.Cells[i].Text == "COM")
            {
                e.Item.Cells[i].BackColor = Color.LightGray;
                [b]e.Item.Cells[i].Text = getCompDate(e.Item.Cells[0].Text, headertext[i]);[/b]
                e.Item.Cells[i].CssClass = "smallPrint";
            }
        }
    }
}


getCompDate(string name, string course) is a funtion that lookups up date the course was completed if the course was completed.

its not pretty, looking back, i could have built alot of this information into my sql statements and pulled using evals, but with dynamic reports into datagrids, i did what i had to do at the time, and its still used today.
 
Thanks I'll give it a go......!
Lbob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top