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!

SelectedItem backcolor

Status
Not open for further replies.

tman24m

Programmer
May 21, 2001
93
US
Hey all,

I'm trying to get the selected item (only the selected item) to have a backcolor of cyan. However, the problem is that I can't use the commandname="Select" method because I've already assigned the commandname property to another command. Here's the code:

<asp:datagrid id=&quot;dgMMCTRN3&quot; style=&quot;Z-INDEX: 101; LEFT: 15px; POSITION: absolute; TOP: 27px&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; GridLines=&quot;None&quot; Height=&quot;73px&quot; Width=&quot;219px&quot; OnItemCommand=&quot;dg_ItemCommand&quot; DataKeyField=&quot;ID&quot; OnItemDataBound=&quot;dtgMMCTRN3_ItemDataBound&quot; ShowFooter=&quot;True&quot;>
<SelectedItemStyle Font-Bold=&quot;True&quot; BackColor=&quot;#FF8080&quot;></SelectedItemStyle>
<AlternatingItemStyle BackColor=&quot;AliceBlue&quot;></AlternatingItemStyle>
<HeaderStyle Font-Bold=&quot;True&quot; BackColor=&quot;SkyBlue&quot;></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<u>Link</u>
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID=&quot;lb1&quot; CommandName=&quot;doProds&quot; Runat=&quot;server&quot; Text='<%# Container.DataItem(&quot;MCCSUM&quot;) %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName=&quot;Select&quot; DataTextField=&quot;MCCTYP&quot; HeaderText=&quot;Typ&quot; HeaderStyle-Font-Underline=&quot;True&quot;></asp:ButtonColumn>




AND THE CODE BEHIND IS:

Sub dg_ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim mo, trn As Double
Dim acct As String
mo = CDbl(Request.QueryString(&quot;postDat&quot;))
trn = CDbl(Session(&quot;LastPost&quot;))
acct = Request.QueryString(&quot;acct&quot;)

If e.CommandName = &quot;doProds&quot; Then
sProduct = CType(e.Item.FindControl(&quot;lb1&quot;), LinkButton).Text
Dim intID As String = dgMMCTRN3.DataKeys(e.Item.ItemIndex)

Dim strSQLDet As String
strSQLDet = &quot;select MCNRRN, MCCSUM, MCCTYP, MCCTRN, MCAJE, MCFDPT, MCFDBT, MCFCRD, CONCAT(CHAR(MCCSUM),CONCAT(MCCTYP,MCCTRN)) AS ID &quot; & _
&quot;from P12582.MMCTRN3 &quot; & _
&quot;where MCFDBT = '&quot; & acct & &quot;' AND MCDGLM = &quot; & mo & &quot; AND MCDTRN = &quot; & trn & &quot; AND CONCAT(CHAR(MCCSUM),CONCAT(MCCTYP,MCCTRN)) ='&quot; & dgMMCTRN3.DataKeys(e.Item.ItemIndex) & &quot;' AND MCFPAG = '1' &quot; & _
&quot;or MCFCRD = '&quot; & acct & &quot;' AND MCDGLM = &quot; & mo & &quot; AND MCDTRN = &quot; & trn & &quot; AND CONCAT(CHAR(MCCSUM),CONCAT(MCCTYP,MCCTRN)) ='&quot; & dgMMCTRN3.DataKeys(e.Item.ItemIndex) & &quot;' AND MCFPAG = '1' &quot;
Dim dr As OleDbDataReader
dr = GetDataReader(strSQLDet)

e.Item.BackColor = Color.Red
dgDetails.DataSource = dr
dgDetails.DataBind()
dr.Close()
End If
End Sub



The item changes to red, but then when another item is selected, the first item retains the backcolor of red
 
There's got to be a better way to do this, but all I can think about now is this (C#): add this loop inside the dg_ItemCommand, instead of e.Item.BackColor = Color.Red line.

foreach(DataGridItem dgItem in dgMMCTRN3.Items)
{
if(dgItem.ItemIndex == e.Item.ItemIndex)
{
dgItem.BackColor = Color.Red;
}
else
{
dgItem.BackColor = Color.White // or whatever default color is
}
}

This will probably achive your goal and highlight the selected row, but like I said, there must be a better way of doing this.
 
thanks for the post.

i added this and still no worky

Dim dgItem As DataGridItem
For Each dgItem In dgMMCTRN3.Items
If dgItem.ItemIndex = e.Item.ItemIndex Then
e.Item.BackColor = Color.Cyan
ElseIf dgItem.ItemIndex <> e.Item.ItemIndex Then
If e.Item.ItemType = ListItemType.Item Then
e.Item.BackColor = Color.White
ElseIf e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.BackColor = Color.AliceBlue
End If
End If
Next
 
When you pick the autoformat builder, it assigns a color to the selected property, does it not???

Try taking a blank form, slap a grid on it, and see how it sets the color. :)
I'm not trying to be smart, it's just what I would try.

I want to do a tutorial when I am done this project titled &quot;How to make your Datagrid behave&quot; LOL

My grid wants to grow in the Y direction.
 
thanks for the post. with the autobuilder, it only assigns the color if you set the commandname property to = &quot;Select&quot;. Problem is, I'm already using the commandname property for something else. Anyway, I just figured it out. I was hasty with my last post. Didn't realize that after I was setting the backcolor of the selected item I was resetting based on whether or not it was an item or alternating item. I moved the selected item down later in the code and now it works fine

Dim dgItem As DataGridItem
For Each dgItem In dgMMCTRN3.Items
If dgItem.ItemIndex <> e.Item.ItemIndex Then
If e.Item.ItemType = ListItemType.Item Then
e.Item.BackColor = Color.White
ElseIf e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.BackColor = Color.AliceBlue
End If
End If
Next
e.Item.BackColor = Color.Cyan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top