当前位置: 首页 > 工具软件 > Databound > 使用案例 >

c# GridVeiw1_DataBound

甄伟兆
2023-12-01

protected void GridVeiw1_DataBound(object sender, GridViewRowEventArgs e)
        {


            if (e.Row.RowType == DataControlRowType.Header)
            {
                //for (int i = 0; i < e.Row.Cells.Count; i++) { e.Row.Cells[i].Text = fnames[i]; e.Row.Cells[i].Attributes.Add("nowarp", "nowarp"); }
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();
                //第一行
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Attributes.Add("colspan", "9");
                tcHeader[0].Attributes.Add("style", "height:35px;text-align:center;");
                tcHeader[0].Text = this.Title + "</th></tr><tr>";

                //第二行
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Text += "编号";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Text += "字段1";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].Text += "字段2";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Text += "字段3";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Text += "字段4";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Text += "字段5";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Text += "字段6";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Text += "字段7";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Text += "字段8</th></tr><tr>";

            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#EDF1D5'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
                //鼠标点击行时设置底色

                //e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
                e.Row.Cells[0].Text = (e.Row.RowIndex + this.GridView1.PageIndex * this.GridView1.PageSize + 1).ToString();

                //3
                //e.Row.Cells[3].Text = e.Row.Cells[3].Text.Trim() == "1" ? "男" : "女";
                //e.Row.Cells[12].Text = e.Row.Cells[12].Text.Trim() == "1" ? "男" : "女";
                //e.Row.Cells[10].Text = e.Row.Cells[10].Text.Trim() == "1" ? "男" : "女";
            }
        }

 类似资料: