C#實現通過單擊gridview中的選擇其中的一行數據

 1.首先拖一個GridView到窗體,並選擇數據源,然後配屬性Colums如下圖:

2.對它事件寫代碼如下:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)     {         GridView _gridView = (GridView)sender;         int _selectedIndex = int.Parse(e.CommandArgument.ToString());         string _commandName = e.CommandName;         if (_commandName == "DownLoad")         {             if (_gridView.SelectedIndex > -1)             {                 _gridView.SelectedRow.ForeColor = System.Drawing.Color.Black;             }             _gridView.SelectedIndex = _selectedIndex;             _gridView.SelectedRow.ForeColor = System.Drawing.Color.Red;         }

    }     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)     {         if (e.Row.RowType == DataControlRowType.DataRow)         {             LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];             string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");             e.Row.Attributes["Onclick"] = _jsSingle;

        }     }

3.重寫  Render(HtmlTextWriter writer) protected override void Render(HtmlTextWriter writer)     {         // The client scripts for GridView1 were created in GridView1_RowDataBound         foreach (GridViewRow r in GridView1.Rows)         {             if (r.RowType == DataControlRowType.DataRow)             {                 Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00");                 Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl01");                 Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl02");             }         }

        base.Render(writer);     }

發佈了57 篇原創文章 · 獲贊 6 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章