各位大侠、牛人们!请教个关于gridview的问题
我希望通过单击gridview的某一行,来获取该行的数据!
也就是说,我想单击gridview的某一行(前提是不启用选择事件),然后该行变色并把该行所有字段的值赋给相应的文本框或者其他能显示数据的控件,然后通过对文本框的值得操作来改变数据库的数据!
希望能给个完整的实例!谢谢!使用sql数据库,DaTaSet数据源操作!
我在网上找了很多!都不没弄出来!
请各位大侠赐教!
谢谢!
[解决办法]
楼主要的情况
有两种理解呀:
你在单击,是在刷新本面的情况下,还是不刷新本页的情况下 把该行所有字段的值赋给相应的文本框
第一种,是你点点击GridView中某一行回传的情况,
在后台实现
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((Button)e.Row.FindControl("btn3")).CommandArgument = e.Row.RowIndex.ToString(); ((Button)e.Row.FindControl("btn2")).CommandArgument = e.Row.RowIndex.ToString(); ((Button)e.Row.FindControl("btn3")).CommandName = "btn3"; } }
[解决办法]
文件名:default6.aspx
满意请结贴,有问题请继续提问,谢谢!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> Price:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>Quantity:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>Sum:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></div> </form> <script type="text/javascript"> function $(sElmID){return document.getElementById(sElmID);} var isIe=window.navigator.appName.indexOf("Netscape") == -1?true:false; var oRows=$("<%=GridView1.ClientID %>").rows; function initRow_Click(){ for(var i=1;i<oRows.length;++i){ oRows[i].onclick=new Function("onRow_Click(this)"); } } function onRow_Click(oSrc){ for(var i=1;i<oRows.length;++i){ oRows[i].style.background=""; } oSrc.style.background="orange"; var oCells=oSrc.getElementsByTagName("td"); $("<%=TextBox1.ClientID %>").value=isIe?oCells[0].innerText:oCells[0].textContent; $("<%=TextBox2.ClientID %>").value=isIe?oCells[1].innerText:oCells[0].textContent; $("<%=TextBox3.ClientID %>").value=isIe?oCells[2].innerText:oCells[0].textContent; } initRow_Click(); </script></body></html>
[解决办法]
html:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("id").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" ToolTip='<%#Eval("id") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("name").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:TextBox ID="TextBox1" runat="server" Width="100px" TextMode="MultiLine"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="100px"></asp:TextBox>
js:
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function showstring(str,str1)
{
var mytext= document.getElementById("TextBox1");
mytext.value=str;
var mytext2= document.getElementById("TextBox2");
mytext2.value=str1;
}
</script>
</head>
cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
void BindGridView()
{
this.GridView1.DataSource = GetTable();
this.GridView1.DataBind();
}
DataTable GetTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
for (int j = 0; j < 10; j++)
{
DataRow row = dt.NewRow();
row["id"] = j;
row["name"] = j+100;
dt.Rows.Add(row);
}
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Label lblid= e.Row.FindControl("lblid") as Label;
Label lblname = e.Row.FindControl("lblname") as Label;
if (lblid != null && lblname != null)
{
e.Row.Attributes.Add("onclick", "return showstring('"+lblid.Text+"','"+lblname.Text+"');");
}
}