如何使用C#在gridview中获取特定数据? [英] How to get a specific data in gridview using C#?

查看:696
本文介绍了如何使用C#在gridview中获取特定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从gridview中检索特定数据。我的数据库由id,type和name等列组成。让我们说我的gridview的内容如下例所示:



Id类型名称



1吉他Ibanez



2吉他吉布森



我想要的是获得Name,Lets下的价值说伊瓦涅斯。我能够使用datakeys检索Id的值,但我无法弄清楚如何在Gridview中获取Name的值。我在下面提供了代码,以便更好地理解我的意思。



我尝试了什么:



I was wondering how to retrieve a specific data from gridview. My database consists of columns like id, type and name. Lets say the contents of my gridview is like this example below:

Id Type Name

1 Guitar Ibanez

2 Guitar Gibson

What i want to happen is to get the value under Name, Lets say "Ibanez". I was able to retrieve the value for Id using datakeys but I can't figure out how to get the value of Name in Gridview. I included code below to better understand what i mean.

What I have tried:

protected void GuitarBrandsGridViewBtn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow gridrow = btn.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[gridrow.RowIndex].Value.ToString());
    con.Open();
    cmd.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id;
    cmd.Connection = con;
    int a = cmd.ExecuteNonQuery();
    con.Close();
    if (a > 0)
    {
        bindgridviewguitarbrands();
    }
    System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanezDetails" + id + ".aspx");
    System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanezDetails" + id + ".aspx.cs");

}

推荐答案

试试这个

try this
int columnNameIndex = 2;
 string name = gridrow.Cells[columnNameIndex].Text;


这篇关于如何使用C#在gridview中获取特定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆