c# - 如何使用c#获取gridview中的特定数据?

标签 c# mysql asp.net gridview aspxgridview

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

ID         类型         名称

1         吉他         伊瓦涅斯

2         吉他         吉布森

我想要发生的是获取名称下的值,让我们说“Ibanez”。我能够使用 datakeys 检索 Id 的值,但我不知道如何获取 Gridview 中的 Name 值。我在下面添加了代码,以便更好地理解我的意思。

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");

}

最佳答案

您可以像这样访问特定列

GuitarBrandsGridView.Rows[gridrow.RowIndex].Cells[1].Text;

关于c# - 如何使用c#获取gridview中的特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265373/

相关文章:

c# - 捕获电子邮件地址中拼写错误的良好正则表达式是什么?

c# - 获取有关 Ninject 拦截的方法的信息

asp.net - 在美国服务器上存储 GMT 日期

c# - GetSystemTimeZones() 的全局化

c# - 如果我们只能为它们分配 null,为什么我们允许将 const 与引用类型一起使用?

c# - 代理调用远程 json 以避免 CORS 问题

c# - 具有一定数量项目的 MemoryCache

mysql - 从日期时间具有相同日期的位置获取结果

mysql - 以最后一条消息的顺序获取消息用户的 SQL 查询

php - 用于从不同表获取数据并将其用于距离计算的 MySQL 查询