1

我的label control页面上有一个,下面label control有一个Gridview。标签控件中存在的值也存在于Gridview. 标签控件具有这样的值

3244|Yellow Ink| Test Link

在gridview中,我也有一个值3244

3244    yello Ink   Test Link
3255    Green Link  Test2

页面加载后,我希望代码中的行索引为 3244。他们有什么办法吗。

4

3 回答 3

0

不知道您拥有什么类型的数据源,一种方法是使用 LINQ,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    // Fetch the text from your label. (I'm assuming that you have only one label with the text "3244|Yellow Ink| Test Link".
    string text = Label1.Text;

    // Find the first row or return null if not found.
    var resultRow = GridView1.Rows.Cast<GridViewRow>().FirstOrDefault(row =>
    {
        // Get the id (that I'm guessing is the first (0-index) column/cell)
        var id = row.Cells[0].Text;

        // Return true/false if the label text starts with the same id.
        return text.StartsWith(id);
    });

    if (resultRow != null)
    {
        var index = resultRow.RowIndex;
    }
}

较短的版本如下所示:

var resultRow = GridView1.Rows.Cast<GridViewRow>()
    .FirstOrDefault(r => text.StartsWith(r.Cells[0].Text));
于 2012-11-13T22:07:01.350 回答
0

在填充 gridView 后的页面加载中,您可以这样做:

   String searchValue = "3244 ";
    int rowIndex = -1;
    foreach (GridViewRow row in GridViewID.Rows)
    {
        if (row.Cells[0].Text.ToString().Equals(searchValue))
        {
            rowIndex = row.RowIndex;
            break;
        }
    }
    int YourIndex = rowIndex;
于 2018-10-09T11:12:45.153 回答
0
  protected void btnJobAppSelected_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridViewJobApplications.Rows)
    {

        int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;

        LinkButton btnJobAppSelected = (LinkButton)GridViewJobApplications.Rows[rowIndex].FindControl("btnJobAppSelected");

    }
}
于 2015-12-01T08:51:12.933 回答