• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我的学习笔记
   首页       联系   管理    订阅  订阅

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();
            }
        }
posted @ 2008-02-20 13:22  吴有鋆  阅读(187)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3