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!

Setting text in DataGrid ButtonColumn based on data row value 1

Status
Not open for further replies.

groston

IS-IT--Management
Dec 31, 2001
141
US
Can't quite wrap my head around this one...

Given a datagrid, based on an underlying DataTable, that has two Button Columns and several bound columns:

<Columns>
<asp:ButtonColumn Text=&quot;Edit&quot; ButtonType=&quot;PushButton&quot; CommandName=&quot;Edit&quot;></asp:ButtonColumn>
<asp:ButtonColumn Text=&quot;Disable&quot; ButtonType=&quot;PushButton&quot; CommandName=&quot;Disable&quot;></asp:ButtonColumn>
<asp:BoundColumn DataField=&quot;AccountName&quot; HeaderText=&quot;Account Name&quot;></asp:BoundColumn>
<asp:BoundColumn DataField=&quot;AccountNumber&quot; HeaderText=&quot;Account Number&quot;></asp:BoundColumn>
</Columns>

Each account has a binary flag to denote if it is active. If the flag is not set, for that row, I would like to
1) Display the row in italics
2) Disable the 'Edit button in column 1
3) Change the label for the button in cloumn 2 from 'Disable' to 'Enable'

The first part is easy:

Private Sub dgAccounts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAccounts.ItemDataBound
If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)) Then
Dim Active As Integer = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, &quot;Active&quot;))
If (Active = 0) Then
e.Item.Font.Italic = True
End If
End If
End Sub

I suspect that the answers to parts two and three are also handled within this function, but I cannot figure this out. Your help is greatly appreciated.


----

Gerry Roston
gerry@pairofdocs.net
 
Substitute first two columns with TemplateColumn - instead of ButtonColumn, it's easier to work with. Then put this logic inside the same ItemDataBound handler, in the IF:
Code:
<Columns>
  <asp:TemplateColumn>
    <ItemTemplate>
       <asp:Button id=&quot;btnEdit&quot; Text=&quot;Edit&quot; CommandName=&quot;Edit&quot; />
    </ItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn>
     <ItemTemplate>
       <asp:Button ID=&quot;btnDisable&quot; Text=&quot;Disable&quot; CommandName=&quot;Disable&quot; />
      </ItemTemplate>
  </asp:TemplateColumn>
</Columns>
.........
Private Sub dgAccounts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAccounts.ItemDataBound
  If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)) Then
    Dim Active As Integer = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, &quot;Active&quot;))
    If (Active = 0) Then
      e.Item.Font.Italic = True
      dim editButton as Button = CType(e.Item.FindControl(&quot;btnEdit&quot;), Button)
      dim disButton as Button = CType(e.Item.FindControl(&quot;btnDisable&quot;), Button)
      editButton.Enabled = False
      disButton.Text = &quot;Enable&quot;
    End If
  End If
End Sub
I think you got the idea. Please check my VB syntax, not sure about it.
 
My guardian programmer comes through again! Thanks!


----

Gerry Roston
gerry@pairofdocs.net
 
As usual, LV was right on target. Just one small fix: 'runat=server' needs to be added to the two <asp:button...> lines.

----

Gerry Roston
gerry@pairofdocs.net
 
That's right... Sorry for missing that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top