GridView_RowDataBound 常用方法


1.這個就不用說,鼠標經過行顏色變化

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      

 if (e.Row.RowType == DataControlRowType.DataRow)//判定當前的行是否屬於datarow類型的行
        {
            //當鼠標放上去的時候 先保存當前行的背景顏色 並給附一顏色
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#7f9edb',this.style.fontWeight='';");
            //當鼠標離開的時候 將背景顏色還原的以前的顏色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }

    }

 

 

 

2.這個也很ez

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)
        {

            switch (e.Row.Cells[1].Text.ToString())
            {
                case "1":
                    e.Row.Cells[1].Text = "加盟司機";
                    break;
                case "2":
                    e.Row.Cells[1].Text = "公司司機";
                    break; 
            }
        }

    }

 

 

 

 

3.行綁定的時候讀取datakeynames中的數據源字段

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataKey datakey = GridView1.DataKeys[e.Row.RowIndex];
            //string DAA_Del = datakey["DAA_Del"].ToString();

            if (datakey["FOC_Del"].ToString() == "True")
            {
                e.Row.Cells[1].Enabled = false;
                e.Row.Cells[2].Enabled = false;
            }
            else
            {
                e.Row.Cells[1].Enabled = true;
                e.Row.Cells[2].Enabled = true;
            }

    }

 

----------------------------------我是分割線----------------------------

查找自定義列中的控件,並改變其屬性

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
{

           CheckBox cb = (CheckBox)e.Row.FindControl("CheckBox2");
            DataKey datakey = GridView1.DataKeys[e.Row.RowIndex];
            if (datakey["endBtid"].ToString() == "")
            {
                cb.Visible = true;
            }
            else
            {
                cb.Visible = false;
            } 
        }

}

    }

 

 

 

 

4.自己寫的一個自增列的數字,前提是不分頁的情況下,簡單

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      

       if (e.Row.RowType == DataControlRowType.DataRow)
        {

        e.Row.Cells[0].Text = Convert.ToString(GridView1.Rows.Count + 1);

        }

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM