WebGrid用法

首先安装Infragistics.NetAdvantage.for.ASP.NET.2007.Vol.2,然后在选择项中添加UltraWebGrid,使用方法如下:
1、添加“总计”值
绑定完数据后,添加如下代码
UltraWebGrid1.Rows.Add();                               
UltraWebGrid1.Rows.Add();                         
UltraWebGrid1.Rows[UltraWebGrid1.Rows.Count - 1].Cells[0].Text = "小计";    //倒数第一行
UltraWebGrid1.Rows[UltraWebGrid1.Rows.Count - 2].Cells[0].Text = "总计";
double sum = 0;
string strtemp = string.Empty;
for (int i = 0; i < UltraWebGrid1.Rows.Count - 1; i++)
       {
      if (UltraWebGrid1.Rows[i].Cells[2].Value != null)
       {
           strtemp = UltraWebGrid1.Rows[i].Cells[2].Value.ToString();
           sum += Convert.ToDouble(strtemp);
         }
        }
UltraWebGrid1.Rows[UltraWebGrid1.Rows.Count - 1].Cells[2].Value = sum;
2、单击单元格选中行
(1) 选择UltraWebGrid-displayout-->CellClickActionDefault=RowSelected
(2) 选择UltraWebGrid—displayout-->SelectedRowStyleDefault的BackColor属性,设置颜色。
3、显示自动列号
RowSelectorsDefault="Yes" AllowRowNumberingDefault="ByDataIsland"
4、隐藏一列
UltraWebGrid1.Columns[i].Hidden=true;
5、添加模板列
(即在绑定数据外,添加的列):先选择UltraWebGrid --属性--columns--勾选Templated column 即可。
6、增加Checkbox
第一种方法:在表格的InitializeRow事件中添加如下代码
if (e.Row.Band.Index == 0)
    {
     string str = string.Empty;
     str = "<input id='chk" + e.Row.Index + "' type='checkbox' name='chkName" + e.Row.Index + " ' />";
     e.Row.Cells[0].Text = str;
        }
第二种方法:
绑定数据后,添加
GridTake.Columns[0].Type = ColumnType.CheckBox;     //设定第0列的数据类型
GridTake.Columns[0].AllowUpdate = AllowUpdate.Yes;//设置checkbox是否可用。
GridTake.Columns.FromKey("CHK").Type= ColumnType.CheckBox;
GridTake.Columns.FromKey("CHK").AllowUpdate = AllowUpdate.Yes;
for (int i = 0; i < GridTake.Rows.Count; i++)
    {
      GridTake.Rows[i].Cells[0].Value = false;     //初始化checkbox
    }
//全选checkbox
protected void cbCheckAll_CheckedChanged(object sender, EventArgs e)
    {
        if (cbCheckAll.Checked)
        {
            for (int i = 0; i < GridTake.Rows.Count; i++)
            {
                GridTake.Rows[i].Cells[0].Value = true;
            }
        }
        else
        {
            for (int i = 0; i < GridTake.Rows.Count; i++)
            {
                GridTake.Rows[i].Cells[0].Value = false;
            }
        }
   
//批量删除数据
if (GridTake.DisplayLayout.SelectedRows.Count < 1)
        {
            Response.Write("<script language='javascript'>alert('请选择要删除的行');</script>");
            return;
        }
        for (int i = 0; i < GridTake.Rows.Count; i++)
        {
            if (GridTake.Rows[i].Cells[0].Value.ToString()=="true")
            {
                try
                {
                    string sql = "delete from test5 where wno='" + GridTake.Rows[i].Cells[2].Value.ToString()  +"'";
                    OracleHelper.ExecuteSql(sql);
                }
                catch (Exception ex)
                {
                    Response.Write("<script language='javascript'>alert('" + ex.Message + "')</script>");
                }
            }
        }
        //GridTake.Rows.Remove(GridTake.DisplayLayout.SelectedRows[0]);
       
        Response.Write("<script language='javascript'>alert('删除成功!');</script>");
        GridDataBind();
删除数据
if (GridTake.DisplayLayout.SelectedRows.Count < 1)
        {
            Response.Write("<script language='javascript'>alert('请选择要删除的行');</script>");
            return;
        }
        try
        {
            string sql = "delete from employees where id='" + GridTake.DisplayLayout.SelectedRows[0].Cells[0].Text + "'";
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            GridTake.Rows.Remove(GridTake.DisplayLayout.SelectedRows[0]);
        }
        catch (Exception ex)
        {
            Response.Write("<script language='javascript'>alert('" + ex.Message + "')</script>");
        }
        Response.Write("<script language='javascript'>alert('数据删除成功!');</script>");

WebGrid中Checkbox的全部选择和取消全部选择
protected void cbCheckAll_CheckedChanged(object sender, EventArgs e)
    {
        if (cbCheckAll.Checked)
        {
            for (int i = 0; i < GridTake.Rows.Count; i++)
            {
                GridTake.Rows[i].Cells[0].Value = true;
            }
        }
        else
        {
            for (int i = 0; i < GridTake.Rows.Count; i++)
            {
                GridTake.Rows[i].Cells[0].Value = false;
            }
        }
    }
WebGrid的固定列功能
具体步骤:
(1) 在UltraWebGrid下的DisplayLayout中把TableLayout属性设为:Fixed ; UseFixedHeaders设为:True
//这是指定Grid允许固定列              StationaryMargins="Header"  //这使Header始终在最上面显示
(2)  设定列固定: Header下面的Fixed设置为true。这样就可以实现ID固定了,还有一个功能就是,点击后面的任何一个列后的“图钉”图标,就可以任意进行多列固定了。如下图为我点击了“ContactName”的效果:这种功能的实现,只要修改一个属性:FixedHeaderIndicatorDefault="Button"就可以了。

中文标题,在绑定数据后,增加
Private void SetGridHeader()
{
//DisplayLayout---view type可调节显示的模式 Flat,Hierarchical, OutlookGroupBy
    UltraWebGrid1.Bands[0].Columns[0].Header.Caption = "序号";
    UltraWebGrid1.Bands[0].Columns[1].Header.Caption = "产品号";
//调节表格除标题以外的表格元素的排列方式
this.UltraWebGrid1.Bands[0].Columns[0].CellStyle.HorizontalAlign = HorizontalAlign.Center;
this.UltraWebGrid1.Bands[0].Columns[1].CellStyle.HorizontalAlign = HorizontalAlign.Left;
//设定表格列的宽度,其中2代表2%,如果是30,代表30%
this.UltraWebGrid1.Bands[0].Columns[0].Width = Unit.Percentage(2);
}
//设定表格的纵向,相同值单元格的合并
//表格中,行rows是从0开始排的,列 cells也是从0开始排的。
//MergerRow代表行,MergerRow = 0代表表格的第一行
//cells[2]代表第三列,意思是我要将第三列有相同数值的单元格合并。
//这个东东放在绑定数据的后面
int MergerRange = 1;
        int MergerRow = 0;
        for (int irow = 1; irow < gridTake.Rows.Count; irow++)
        {
            if (gridTake.Rows[MergerRow].Cells[2].Text == gridTake.Rows[irow].Cells[2].Text)
            {
                MergerRange++;           //MergerRange代表要合并的单元数量。
            }
            else
            {
                gridTake.Rows[MergerRow].Cells[2].RowSpan = MergerRange;
                MergerRow = irow;
                MergerRange = 1;
            }
            if (irow == gridTake.Rows.Count - 1)
            {
                gridTake.Rows[MergerRow].Cells[2].RowSpan = MergerRange;
            }
        }
表格中超级链接的设定:
//rowloop控制表格中行的移动
//Cells[0]表示第一列的各项要设置超链接。
//facxTC.aspx链接的文件
for (int rowLoop = 0; rowLoop < QryWebGrid.Rows.Count; rowLoop++)
   {
     if (QryWebGrid.Rows[rowLoop].Cells[0].Text != "")
        {
     QryWebGrid.Rows[rowLoop].Cells[0].Text = "<a style='cursor:hand' onclick=\"window.open('FacxTC.aspx?DetailItemNo=" + QryWebGrid.Rows[rowLoop].Cells[1].Text
     + "&No=3"
     + "',null,'height=910px,width=1000px,top=50, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no') ;  \"><u>"
     + QryWebGrid.Rows[rowLoop].Cells[0].Text + "</u></a>";
         }
}
//修改表格数据时,将选择的表格数据显示在文本框或其他控件中
private void GridToText()
    {
        txtUserName.Text = GridTake.DisplayLayout.ActiveRow.Cells[1].ToString();
        txtUserPass.Text = GridTake.DisplayLayout.ActiveRow.Cells[2].ToString();
        rdoSex.SelectedValue = GridTake.DisplayLayout.ActiveRow.Cells[3].ToString();
}
//文本框的值传到表格上
private void InsertGrid()
    {
        GridTake.Rows.Add();
        int rownum = GridTake.Rows.Count-1;
        GridTake.Rows[rownum].Cells[0].Value = TextBox1.Text;
        GridTake.Rows[rownum].Cells[1].Value = TextBox1.Text;
        GridTake.Rows[rownum].Cells[2].Value = TextBox1.Text;
}

//Webgrid添加checkbox批量删除
设置UltraWebGrid的第0列为模板列(UltraWebGrid --属性--columns--勾选Templated column 即可),加入CheckBox控件(Behavior--type下选择Checkbox),判断其状态是否被选中,代码如下
//可以写在任何地方,属于通用代码
Infragistics.WebUI.UltraWebGrid.TemplatedColumn    tcol   =   (TemplatedColumn)UltraWebGrid1.Bands[0].Columns[0];//模板列
foreach(CellItem    item   in    tcol.CellItems) 
   {             
           System.Web.UI.WebControls.CheckBox    chkIsHaveRight   =    (System.Web.UI.WebControls.CheckBox)item.FindControl("chbShenhe"); 
          if(chkIsHaveRight.Checked  ==  true) 
           { 
                  //进行相关的操作
            }
   }

你可能感兴趣的:(grid)