ورود

View Full Version : سوال: رنگی شدن سطر انتخابی گریدویو



mahsa.n
دوشنبه 22 اسفند 1390, 00:03 صبح
سلام

من یه گریدویو دارم میخوام وقتی روی سطری از اون کلیک میشه رنگ پس زمینه سطر مثلا آبی بشه، کد زیر رو گذاشتم ولی اجرا نمیشه لطفا راهنمایی کنید



<asp:GridView ID="grdArticleGroups" runat="server" AutoGenerateColumns="False" DataKeyNames="ArticleGroupId"
DataSourceID="ArticleGroupsSqlDataSource"
ShowHeader="false"
OnRowDataBound="grdArticleGroups_RowDataBound" onselectedindexchanged="grdArticleGroups_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ArticleGroupId" HeaderText="ArticleGroupId" ReadOnly="True"
SortExpression="ArticleGroupId" Visible="false" />
<asp:BoundField DataField="ArticleGroupTitle" HeaderText="ArticleGroupTitle" SortExpression="ArticleGroupTitle" />
<asp:CommandField ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn"/>
</Columns>

</asp:GridView>



protected void grdArticleGroups_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdArticleGroups.SelectedRow;
row.BackColor = System.Drawing.Color.Blue;

}

hjran abdpor
دوشنبه 22 اسفند 1390, 00:28 صبح
سلام.
میتونید از چندین روش برای اینکار استفاده کنید ، از CSS , JavaScript , Ajax , Jquery استفاده کنید .
<script type="text/javascript"> var gridClientId = '<%=YourGridId.ClientID %>'; /* getting Gridview's ClientId */

$(document).ready(function () {

$('#' + gridClientId + ' tbody tr').hover(function () {

$(this).addClass('HoverRow'); /* HoverRow is a css class*/

},function () { $(this).removeClass('HoverRow'); }).click(function () {

/* This IF condition is for highlight the selected row until next row is selected */
if ($('#' + gridClientId + ' tbody tr').hasClass('HoverClick')) {
$('#' + gridClientId + ' tbody tr').removeClass('HoverClick');
$(this).addClass('HoverClick');
} else {
$(this).addClass('HoverClick');
} }) } } </Script> CSS class ---------- .HoverRow
{
cursor:hand;
background-color:Black;
color:Red;

}
.HoverClick
{

color:#0000FF;
background-color:Red;
font-weight:bold;
}


CSS & Jquery


for highlight
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
row.BackColor = System.Drawing.Color.SomeColor;
}
for hover:
<div> protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onMouseOver", "this.style.background='#yourColor'");
e.Row.Attributes.Add("onMouseOut", "this.style.background='#yourOtherColor'");
}


تایپک اقای راد را هم یادتون نره سر بزنید کامل و عالی :
http://barnamenevis.org/showthread.php?50594

موفق باشید.

maryam_vb
دوشنبه 22 اسفند 1390, 11:07 صبح
<script type="text/javascript">
//variable that will store the id of the last clicked row
var previousRow;

function ChangeRowColor(row) {
//If last clicked row and the current clicked row are same
if (previousRow == row)
return; //do nothing
//If there is row clicked earlier
else if (previousRow != null)
//change the color of the previous row back to white
document.getElementById(previousRow).style.backgro undColor = "#ffffff";

//change the color of the current row to light yellow

document.getElementById(row).style.backgroundColor = "#ffffda";
//assign the current row id to the previous row id
//for next row to be clicked
previousRow = row;
}
</script>


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "ChangeRowColor('" + e.Row.RowIndex+ "')");
}
}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
int rowId = e.Row.RowIndex;
e.Row.Attributes.Add("id", rowId.ToString());
}

mahsa.n
دوشنبه 22 اسفند 1390, 21:09 عصر
چه جوری دو سطر کد زیر رو با هم ادغام کنم؟


e.Row.Attributes.Add("onclick", "ChangeRowColor('" + e.Row.RowIndex + "')");
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdAr ticleGroups, "Select$" + e.Row.RowIndex);