GridView

public   partial   class  GridView_GridView3 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
for (int i = 1; i < GridView1.Columns.Count; i++)
            
{
                DropDownList1.Items.Add(i.ToString());
            }

           
  }

    }

    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#00ffcc'");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=c");
        }

    }



    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        
{
            
string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "au_fname"));
            
if (lbl == "myname")
            
{
                e.Row.BackColor 
= System.Drawing.Color.LawnGreen;
            }

        }

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
        GridView1.Columns[columns].Visible 
= false;
    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
for (int i = 0; i < GridView1.Columns.Count; i++)
        
{
            GridView1.Columns[i].Visible 
= true;
        }

    }

    
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    
{
        
foreach (GridViewRow gr in GridView1.Rows)
        
{
            CheckBox ckb 
= (CheckBox)gr.Cells[6].FindControl("CheckBox2");

            
if (!ckb.Checked)
            
{
                ckb.Checked 
= true;
            }

            
else
            
{
                ckb.Checked 
= false;
            }

        }

    }

    
protected void Button3_Click(object sender, EventArgs e)
    
{
        
foreach (GridViewRow gr in GridView1.Rows)
        
{
            CheckBox ckb 
= (CheckBox)gr.Cells[6].FindControl("CheckBox2");
            
if (ckb.Checked)
            
{
                
string id = gr.Cells[1].Text;
                
string delete = "delete  from authors where au_id='" + id + "'";
                db sdb 
= new db();
                sdb.sql(delete);
                GridView1.DataBind();
            }

        }

    }

}


重点:
1、 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00ffcc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }

如果行类型是数据行,添加自定义属性
2、 for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "au_fname"));
            if (lbl == "myname")
            {
                e.Row.BackColor = System.Drawing.Color.LawnGreen;
            }
        }

3、 foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox ckb = (CheckBox)gr.Cells[6].FindControl("CheckBox2");
            if (ckb.Checked)
            {
                string id = gr.Cells[1].Text;
                string delete = "delete  from authors where au_id='" + id + "'";
                db sdb = new db();
                sdb.sql(delete);
                GridView1.DataBind();
            }
        }

你可能感兴趣的:(GridView)