如何选中GridView中的某一行 本帖最后由 lincole 于 2009-11-17 10:27:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以给Button的ArgumentValue属性绑定当前行的索引(数据库里的主键列),然后在后台的按钮OnCommand事件里用e.ArgumentValue就可以获得按钮绑定的数据了。 在DataGridView中设置一个隐藏列,记录是否选中。通过cellclick事件改变隐藏列的值。从而控制颜色。 在RowDateBound事件里写如下CS代码: if (e.Row.RowType == DataControlRowType.DataRow) { string headID = e.Row.Cells[0].Text.Trim(); string modelstring = "javascript:changeRowColor(" + e.Row.RowIndex + ",'" + gvMainList.ClientID + "');"; }"; //双击 事件 e.Row.Attributes.Add("OnDblClick", modelstring); e.Row.Attributes.Add("onclick", "changeRowColor(" + e.Row.RowIndex + ",'" + gvMainList.ClientID + "');"); //设置悬浮鼠标指针形状为"小手" e.Row.Attributes["style"] = "Cursor:hand"; }上面的JS方法内容 function changeRowColor1( row,girdview) { var GridView = document.getElementById(girdview); if(GridView!=null) { //GridView.rows[1].className ="RowStyle"; for(i = 1;i < GridView.rows.length; i++) { if(i==row+1) { GridView.rows[i].className ="SelectedRowStyle1"; } else { if(i%2==0) { GridView.rows[i].className ="RowStyleNo"; } else { GridView.rows[i].className ="RowStyle"; } } } } else { return false; } }.SelectedRowStyle1{ font-family: 宋体; font-size: 12px; border-style: none; background-color: #FF8282; color: #000000; font-weight: normal; height: 16px;}.RowStyleNo{ border-style:none; border:1px; background-color: #F7F7F7; font-family: 宋体; font-size: 12px; color: #000000; height: 16px;}.RowStyle{ border-style:none; border:1px; background-color: #F1F8FE; font-family: 宋体; font-size: 12px; font-weight:normal; color: #000000; height: 16px; } 后台protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) {e.Row.Attributes.Add("onclick", "setIndexValue("+e.Row.DataItemIndex+");");}}前台放一个hidden<input type="hidden" id="selectedindex" runat="server" />js函数function setIndexValue(index){document.getElementById("selectedindex").value = index;}后台button事件protected void button1_Click(object sender, EventArgs e){int index = Convert.ToInt32(selectedindex.Value);if(index<gridview.Rows.Count){GridViewRow row = gridview.Rows[index];}} my97datepicker日期选择框的“年、月”的位置 WCF中服务的操作能同时支持GET和POST吗 在Webform中如何获得UserControl的静态HTML代码? 正则表达式怎么写! asp.net如何判断输入的是手机号 怎么加载Microsoft.Office.Interop.Excel 求半角符号转全角符号的函数! 各位老大,问一个超菜的问题,来者有份 vs 2005 beta版 求大神!!!VS2012 aspx页面代码显示灰色 asp.net乱码问题? img.src = 'file:///' + v; firefox 的确有很严重的问题,如何修改
通过cellclick事件改变隐藏列的值。
从而控制颜色。
{
string headID = e.Row.Cells[0].Text.Trim();
string modelstring = "javascript:changeRowColor(" + e.Row.RowIndex + ",'" + gvMainList.ClientID + "');";
}"; //双击 事件
e.Row.Attributes.Add("OnDblClick", modelstring);
e.Row.Attributes.Add("onclick", "changeRowColor(" + e.Row.RowIndex + ",'" + gvMainList.ClientID + "');");
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
}
上面的JS方法内容 function changeRowColor1( row,girdview)
{
var GridView = document.getElementById(girdview);
if(GridView!=null)
{
//GridView.rows[1].className ="RowStyle";
for(i = 1;i < GridView.rows.length; i++)
{
if(i==row+1)
{
GridView.rows[i].className ="SelectedRowStyle1";
}
else
{
if(i%2==0)
{
GridView.rows[i].className ="RowStyleNo";
}
else
{
GridView.rows[i].className ="RowStyle";
}
}
}
}
else
{
return false;
}
}.SelectedRowStyle1
{
font-family: 宋体;
font-size: 12px;
border-style: none;
background-color: #FF8282;
color: #000000;
font-weight: normal;
height: 16px;
}
.RowStyleNo
{
border-style:none;
border:1px;
background-color: #F7F7F7;
font-family: 宋体;
font-size: 12px;
color: #000000;
height: 16px;
}
.RowStyle
{
border-style:none;
border:1px;
background-color: #F1F8FE;
font-family: 宋体;
font-size: 12px;
font-weight:normal;
color: #000000;
height: 16px;
}
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "setIndexValue("+e.Row.DataItemIndex+");");
}
}
前台
放一个hidden
<input type="hidden" id="selectedindex" runat="server" />
js函数
function setIndexValue(index)
{
document.getElementById("selectedindex").value = index;
}
后台button事件
protected void button1_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(selectedindex.Value);
if(index<gridview.Rows.Count)
{
GridViewRow row = gridview.Rows[index];
}
}