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

DataGrid Column Help, please

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
My datagrid has a few columns that display correctly. The are bound meaning I do not set the auto generate at run time. The are bound columns, not template columns.

All that said. Why does this not work. I am trying to format my columns.

Private Sub dgGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgGrid.ItemDataBound
Dim lblField = CType(e.Item.FindControl("CustName"), Label)
lblField.text = lblField.text & "-Test"
End Sub

I get an object variable or with block variable not fround on lblField.text = lblField.text & "-Test" line.

Any ideas on where to check for this? I know the column name is good because it displays.

And why is the column considered a label control?
 
The only reason I can see is that your Label is not named "CustName", but probably already checked for it.
I'm using the same technique in several places in my app and the code looks very similar to yours (in C#):

private void bindDeleteColumn(object sender, System.Web.UI.WebControls.DataGridItemEventArgs bindArgs)
{

LinkButton lb;
if(bindArgs.Item.ItemType == ListItemType.Item || bindArgs.Item.ItemType == ListItemType.AlternatingItem)
{
lb = ((LinkButton)bindArgs.Item.FindControl("del_EventType"));
lb.Attributes.Add("onclick", "javascript:return confirmDelete();");
}
}

 
Label.... So when the grid displays, it does so with labels??
 
Well, in your codebehind you are looking for a Label control, placed inside one of the DataGrid:

Dim lblField = CType(e.Item.FindControl("CustName"), Label)

so your HTML probably looks like this:

<asp:BoundColumn>
<ItemTemplate>
<asp:Label ID=&quot;CustName&quot; runat=server />
</ItemTemplate
</asp:BoundColumn>

And if it's not a Label in your grid then you'll get an error.
 
No. There is no label. It's just like this...

<asp:BoundColumn DataField=&quot;CustName1&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Customer Name&quot;>
<HeaderStyle Width=&quot;250px&quot;></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField=&quot;CustAddr1&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;Address&quot;>
<HeaderStyle Width=&quot;250px&quot;></HeaderStyle>
</asp:BoundColumn>

Think that's what is wrong??? :)
 
Oh, sorry, <ItemTemplate> applies for <asp:TemplateColumn> only.
Your code though is looking for a Label control, as if you had it inside a TemplateColumn.
Not sure how you get a value out of a BoundColumn but you can do it pretty easy if you switch to TemplateColumn:

<Columns>
<asp:TemplateColumn HeaderText=&quot;Customer Name&quot;>
<HeaderStyle Width=&quot;250px&quot;></HeaderStyle>
<ItemTemplate>
<asp:Label ID=&quot;CustName&quot; runat=server Text='<%# DataBinder.Eval(Container.DataItem, &quot;CustName1&quot;) %>' />
</ItemTemplate
</asp:TemplateColumn>
</Columns>

The grid will display will not change but the code inside your dgGrid_ItemDataBound event handler will find a Label control, called CustName, in each of the rows and change its text as you want it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top