怎么获取gridview绑定行某列的值

 我来答
就烦条0o
2016-07-16 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46484
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
获取GridView里某行某列的值示例
//GridView设置
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" Font-Size="9pt" ForeColor="#333333" GridLines="None"
Width="500px" OnRowDeleting="GridView1_RowDeleting"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" PageSize="6" Height="1px">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<Columns>//codego.net/tags/11/1/
<asp:BoundField DataField="ST_n_id" HeaderText="文章ID" />
<asp:BoundField DataField="ST_n_title" HeaderText="文章主题" />
<asp:BoundField DataField="ST_n_hit" HeaderText="人气指数" />
<asp:BoundField DataField="ST_n_re" HeaderText="评论" />
<asp:HyperLinkField DataNavigateUrlFields="ST_n_id" DataNavigateUrlFormatString="ViewContent.aspx?id={0}"
HeaderText="查看信息" Text="查看信息" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Width="9px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
//加载数据库数据
SqlData da = new SqlData();
private static string ID;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/*判断是否登录*/
ST_check_Login();
}
//定义一个查询用户信息的SQL语句
string sqlstr = "select * from tb_Blog where UserName='" + Session["UserName"] + "'";
//调用公共类中的GetDataSet方法执行SQL语句,并返回DataSet类型的数据集
DataSet mydataset = da.GetDataSet(sqlstr, "tb_Blog");
//创建数据表的视图
DataRowView rowview1 = mydataset.Tables["tb_Blog"].DefaultView[0];
//获取注册博客的编号
ID = rowview1["BlogID"].ToString();
//调用自定义绑定方法绑定数据
ST_Article_Bind();
}
//绑定数据库显示数据
public void ST_Article_Bind()
{
//根据获取的用户ID查询其所有文章
string sqlstr = "select * from ST_news where BlogID='" + ID + "'";
//调用公共类中的ExceDS方法,返回一个DataSet类型的数据集,并作为GridView控件的数据源
GridView1.DataSource = da.ExceDS(sqlstr);
//获取GridView控件中的主键字段
GridView1.DataKeyNames = new string[] { "ST_n_id" };
//从数据库中绑定数据到列表控件中
GridView1.DataBind();
}
//查看某行某列信息跳转ViewContent.aspx
<asp:Label ID="labSubject" runat="server" Width="316px" Font-Size="9pt" ></asp:Label>//显示主题
<asp:TextBox ID="txtContent" runat="server" Font-Size="9pt" Height="257px" TextMode="MultiLine"
Width="405px"></asp:TextBox>//显示内容
//加载绑定数据库内容显示数据
protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
try
{
string str;
string str2 = Page.Request["id"].ToString();
//此处用来实现将数据绑定到前台
SqlConnection mycon = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
mycon.Open();
SqlDataAdapter myada = new SqlDataAdapter("select * from ST_news where ST_n_id='" + str2 + "'", mycon);
DataSet ds = new DataSet();
myada.Fill(ds, "ST_news");
//创建DataRowView对象的一个实例
DataRowView rowview = ds.Tables["ST_news"].DefaultView[0];
//将文章主题读取到labSubject文本框中
this.labSubject.Text = rowview["ST_n_title"].ToString();
//将文章内容读取到txtContent文本框中
this.txtContent.Text = rowview["ST_n_content"].ToString();
mycon.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

}

GridView1.Rows[e.NewSelectedIndex].Cells[4].Text
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式