0
我使用網格視圖和裏面即時通訊使用下拉列表如何在運行時更改gridview行顏色?
我的GridView
<asp:GridView ID="grdUser" runat="server" AllowPaging="True" AllowSorting="True" CaptionAlign="Left" OnPageIndexChanging="grdUser_PageIndexChanging" OnSorting="grdUser_Sorting" PageSize="5" CssClass="table table-hover table-striped table-responsive" DataKeyNames="Email">
<AlternatingRowStyle BackColor="#CCCCFF" />
<HeaderStyle BackColor="#009933" Font-Bold="True" Font-Size="Medium" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerSettings FirstPageText="First" Mode="NumericFirstLast" LastPageText="Last" PageButtonCount="4" NextPageText="" />
<PagerStyle BackColor="#CC66FF" HorizontalAlign="Right" VerticalAlign="Middle" ForeColor="Black" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlAct" runat="server" OnSelectedIndexChanged="ddlAct_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="Action" Text="imtiaz" >Action</asp:ListItem>
<asp:ListItem Value="Activate" Text="imtiaz">Activate</asp:ListItem>
<asp:ListItem Value="Block" Text="imtiaz">Block</asp:ListItem>
<asp:ListItem Value="Delete" Text="imtiaz">Delete</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
現在還當我點擊激活選定行的顏色應該改變
的情況我代碼是
protected void ddlAct_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(CS))
{
DropDownList ddlAct = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddlAct.Parent.Parent;
int idx = row.RowIndex;
string Email = grdUser.DataKeys[idx]["Email"].ToString();
//string Email = ((Label)row.Cells[0].FindControl("Email")).Text;
DropDownList ddl = (DropDownList)row.Cells[0].FindControl("ddlAct");
var ddlvalue = "";
if (ddl.Text.ToString() == "Activate")
{
ddlvalue = "Approved";
}
else if (ddl.Text.ToString() == "Block")
{
ddlvalue = "Blocked";
}
else
{
ddlvalue = ddl.Text.ToString();
}
string query = "Update Users set Status ='" + ddlvalue + "' where E_Mail='" + Email + "'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();
GetDataTable();
lblMsg.Visible = true;
lblMsg.ForeColor = Color.Red;
lblMsg.Text = "Status is : Account is " + ddl.Text.ToString();
}
}
我必須改變只在選定的索引內更改顏色。 請幫助
小改進:使用'ddlAct.NamingContainer'而不是'ddlAct.Parent.Parent'和'row.FindControl(「ddlAct」)'而不是'row.Cells [0] .FindControl(「ddlAct」)'。 –
根據這個問題,目前尚不清楚。爲什麼你不能使用:'row.BackColor = whatevercolor' –
你嘗試過'grdUser.SelectedRow.BackColor = System.Drawing.Color.Aqua'嗎? –