0

このコードを使用して、GridView の各行に一意の ID を付与しています

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   GridViewRow row = e.Row;
   if (row.RowType == DataControlRowType.DataRow)
   {
      row.Attributes["id"] = cRowID.ToString();
      cRowID++;
   }   
}

ここで、cRowIDはグローバル整数変数です。
このコードは、次の HTML コードを提供します。

<table id="gridview1">
 <tr id="1"><td>...</td></tr>
 <tr id="2"><td>...</td></tr>
  .
  .  
 <tr id="n"><td>...</td></tr>
</table>

cRowID を、同じ行に追加される特定の列 (column1 など) の値に置き換えるにはどうすればよいですか?

HTMLにtrタグのIDを追記してからEDIT
になりたい

<tr id="abc" ><td>abc</td><td>...</td></tr>
<tr id="pqr" ><td>pqr</td><td>...</td></tr>
<tr id="xyz" ><td>xyz</td><td>...</td></tr>

出来ますか?はいの場合、その方法を説明してください。

4

2 に答える 2

5

テスト済み これを試す

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
     int counter=1;
     foreach (GridViewRow oItem in GridView1.Rows)
     { 
        counter++;
        oItem.Cells[0].Text = counter.ToString();
        oItem.Attributes.Add("id", "yourvalue");//here you can set row id ie(<tr>)
     }
   }

編集済み

foreach (GridViewRow oItem in GridView1.Rows)
     { 
        string getFirstColValue = oItem.Cells[0].Text;
        oItem.Attributes.Add("id", getFirstColvalue );
     }
于 2012-08-25T09:37:16.203 に答える
0

ループを通過する必要はありません。以下のコードは正常に動作しています。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string getFirstColValue = e.Row.Cells[0].Text;
        e.Row.Attributes.Add("id", getFirstColvalue );            
    }
}
于 2014-01-31T07:00:05.600 に答える