iT邦幫忙

0

ASP.net C# 如何將GridView1 的值傳到GridView2 ItemTemplate TextBox _已解決

各位前輩大家好,小弟是ASP新手還希望各位前輩不吝指教
我做了一個GridView1_SelectedIndexChanged的事件
主要是如果滑鼠點選GridView1其中一個Row值
就把值帶到GridView2固定的五格欄位中
Row[0].cell[0],Row[1].cell[0],
Row[2].cell[0],Row[3].cell[0],Row[4].cell[0]
目前是用RowIndex的方法填值,但GridView2五個欄位都會被填到值
想請問要怎麼去下判斷?? 還是有更好的方法呢??
https://ithelp.ithome.com.tw/upload/images/20180323/20107444VZ1mQuMutp.jpg

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
  GridViewRow row = GridView5.SelectedRow;
  if (row != null)
  {
    foreach (GridViewRow row2 in GridView3.Rows)
    {
      if (row2.RowType == DataControlRowType.DataRow)
      {
        string tr = row2.RowIndex.ToString();
        TextBox p_Anti_name = (TextBox)row2.FindControl("txtAnti_name");
        p_Anti_name.Text = (row.FindControl("lblDr_Name") as Label).Text;
       }
     }
   }
}

1 個回答

0
暐翰
iT邦大師 1 級 ‧ 2018-03-23 09:38:20
最佳解答

可以改成

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
  GridViewRow row = GridView5.SelectedRow;
  if (row != null)
  {
    foreach (GridViewRow row2 in GridView3.Rows)
    {
      if (row2.RowType == DataControlRowType.DataRow)
      {
        TextBox p_Anti_name = (TextBox)row2.FindControl("txtAnti_name");
        //如果沒被填過值,更新資料並返回
        if (null == p_Anti_name.Text || "".Equals(p_Anti_name.Text) )
        {
          p_Anti_name.Text = (row.FindControl("lblDr_Name") as Label).Text;
          return;
        }
      }
    }
  }
}

原理:

foreach GridView3時檢查是否有被修改過,假如沒有才修改並返回


我沒測試過,先簡單提供邏輯,假如有問題跟我說 :)

我要發表回答

立即登入回答