简要的GRIDVIEW

    private static string Ids="";

    protected void Page_Load(object sender,EventArgs e)
    {
        if (!this.IsPostBack)
            BindData("select * from myTable");
    }

    private void BindData(string sql)
    {
        DataSet ds = GetDataSet(sql);
        DataView dv = ds.Tables[0].DefaultView;
        //dv.Sort = "dt desc";
        GridV.DataSource = dv;// GetDataSet(sql);
        GridV.DataBind();
    }

    protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridV.EditIndex = e.NewEditIndex;
        BindData("select * from myTable");

        SetBindedClientScript(e);
    }

    protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridV.EditIndex = -1;
        BindData("select * from myTable");
    }

    private void SetBindedClientScript(GridViewEditEventArgs e)
    {
        string js = @"<script language=javascript>";
        js += "<!-- function SetJs() {";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
        js += "};";
        js += "setTimeout(SetJs,1);";
        js += "//--></script>";

        if (!this.IsStartupScriptRegistered("clientjs"))
            this.RegisterStartupScript("clientjs", js);
    }

    protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
            if(del.Text.ToString()=="删除")
               del.Attributes["onclick"] = "return confirm('确定要删除?')";
        }
    }

    protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
        if (Ids == "")
            Ids += aId;
        else
            Ids += "," + aId;
        string sql = "select * from myTable where id not in(" + Ids.ToString()+")";

        BindData(sql);
    }

    /// <summary>
    ///得到DATASET
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    protected DataSet GetDataSet(string sql)
    {
        string StrCon = "";
        StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
        OleDbConnection Conn = new OleDbConnection(StrCon);
        OleDbCommand Cmd = Conn.CreateCommand();
        Cmd.CommandText =sql;

        Conn.Open();
        //Cmd.ExecuteNonQuery();

        OleDbDataAdapter Adpt = new OleDbDataAdapter();
        Adpt.SelectCommand = Cmd;
        DataSet dst = new DataSet();
        Adpt.Fill(dst);
        Conn.Close();
        return dst;
    }

    protected void btnFirstPage_Click(object sender, EventArgs e)
    {

        GridV.PageIndex =0;
        BindData("select * from myTable");
    }
    protected void btnPreviousPage_Click(object sender, EventArgs e)
    {
        if ((GridV.PageIndex-1) < 0)
            return;
       
        GridV.PageIndex-=1;
        BindData("select * from myTable");
    }
    protected void btnNextPage_Click(object sender, EventArgs e)
    {
        if (GridV.PageIndex > GridV.PageCount)
            return;
       
        GridV.PageIndex += 1;
        BindData("select * from myTable");
    }
    protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridV.PageIndex =GridV.PageCount-1;
        BindData("select * from myTable");
    }
    protected void GridV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridV.PageIndex = e.NewPageIndex;
        BindData("select * from myTable");
    }
    protected void GridV_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataSet ds = GetDataSet("select * from myTable");
        DataView dv = ds.Tables[0].DefaultView;
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "asc";
        else
        {
            if (ViewState["SortDirection"].ToString() == "asc")
                ViewState["SortDirection"] = "desc";
            else
                ViewState["SortDirection"] = "asc";
        }
        dv.Sort = e.SortExpression.ToString() + " " + ViewState["SortDirection"].ToString();
        GridV.DataSource = dv;
        GridV.DataBind();
    private static string Ids="";

    protected void Page_Load(object sender,EventArgs e)
    {
        if (!this.IsPostBack)
            BindData("select * from myTable");
    }

    private void BindData(string sql)
    {
        DataSet ds = GetDataSet(sql);
        DataView dv = ds.Tables[0].DefaultView;
        //dv.Sort = "dt desc";
        GridV.DataSource = dv;// GetDataSet(sql);
        GridV.DataBind();
    }

    protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridV.EditIndex = e.NewEditIndex;
        BindData("select * from myTable");

        SetBindedClientScript(e);
    }

    protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridV.EditIndex = -1;
        BindData("select * from myTable");
    }

    private void SetBindedClientScript(GridViewEditEventArgs e)
    {
        string js = @"<script language=javascript>";
        js += "<!-- function SetJs() {";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
        js += "};";
        js += "setTimeout(SetJs,1);";
        js += "//--></script>";

        if (!this.IsStartupScriptRegistered("clientjs"))
            this.RegisterStartupScript("clientjs", js);
    }

    protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
            if(del.Text.ToString()=="删除")
               del.Attributes["onclick"] = "return confirm('确定要删除?')";
        }
    }

    protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
        if (Ids == "")
            Ids += aId;
        else
            Ids += "," + aId;
        string sql = "select * from myTable where id not in(" + Ids.ToString()+")";

        BindData(sql);
    }

    /// <summary>
    ///得到DATASET
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    protected DataSet GetDataSet(string sql)
    {
        string StrCon = "";
        StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
        OleDbConnection Conn = new OleDbConnection(StrCon);
        OleDbCommand Cmd = Conn.CreateCommand();
        Cmd.CommandText =sql;

        Conn.Open();
        //Cmd.ExecuteNonQuery();

        OleDbDataAdapter Adpt = new OleDbDataAdapter();
        Adpt.SelectCommand = Cmd;
        DataSet dst = new DataSet();
        Adpt.Fill(dst);
        Conn.Close();
        return dst;
    }

    protected void btnFirstPage_Click(object sender, EventArgs e)
    {

        GridV.PageIndex =0;
        BindData("select * from myTable");
    }
    protected void btnPreviousPage_Click(object sender, EventArgs e)
    {
        if ((GridV.PageIndex-1) < 0)
            return;
       
        GridV.PageIndex-=1;
        BindData("select * from myTable");
    }
    protected void btnNextPage_Click(object sender, EventArgs e)
    {
        if (GridV.PageIndex > GridV.PageCount)
            return;
       
        GridV.PageIndex += 1;
        BindData("select * from myTable");
    }
    protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridV.PageIndex =GridV.PageCount-1;
        BindData("select * from myTable");
    }
    protected void GridV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridV.PageIndex = e.NewPageIndex;
        BindData("select * from myTable");
    }
    protected void GridV_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataSet ds = GetDataSet("select * from myTable");
        DataView dv = ds.Tables[0].DefaultView;
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "asc";
        else
        {
            if (ViewState["SortDirection"].ToString() == "asc")
                ViewState["SortDirection"] = "desc";
            else
                ViewState["SortDirection"] = "asc";
        }
        dv.Sort = e.SortExpression.ToString() + " " + ViewState["SortDirection"].ToString();
        GridV.DataSource = dv;
        GridV.DataBind();
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在GridView中,可以通过设置列宽来控制每个列的大小和排列。要设置GridView的列宽,可以使用GridView的columns属性。 首先,在XML布局文件中,定义GridView控件,并给它设置一个唯一的ID,如"myGridView"。接着,在Activity中,找到该GridView控件,并给它设置Adapter。 然后,在代码中,实例化一个GridView对象,通过findViewById方法找到GridView的ID。接着,创建一个Adapter对象,并将其设置为GridView的Adapter。最后,通过GridView的columns属性,设定每个列的宽度。 如需设置列宽,可在布局文件的GridView节点中,添加以下属性: android:stretchMode="none" // 默认为列宽平均分配,设置为none后才能调整列宽 然后,在Activity的Java代码中,可以使用GridView的setColumnWidth方法来调整列宽。该方法接受一个整数值作为参数,表示列的宽度,单位为像素。例如,若要将第一列的宽度设置为100像素,则可以通过以下代码实现: myGridView.setColumnWidth(0, 100); 若想要让所有列的宽度都相同,可以使用GridView的setNumColumns方法。该方法接受一个整数值作为参数,表示需要显示的列数。例如,若要显示3列,并且每列的宽度相同,则可以通过以下代码实现: myGridView.setNumColumns(3); 以上是关于如何在GridView中设置列宽的简要解释。通过使用GridView的columns属性和相应的方法,可以根据需要自定义每个列的宽度,以实现更好的展示效果。 ### 回答2: GridView是Android开发中常用的控件之一,用于展示数据。在GridView中,我们可以通过设置列宽来控制每个单元格的宽度。 要设置GridView的列宽,可以通过编程方式或者在XML布局文件中进行设置。 如果使用编程方式,我们可以通过获取GridView的列数,并根据列数计算每个单元格的宽度。具体步骤如下: 1.首先,获取GridView的实例。 2.获取GridView的列数。可以使用getNumColumns()方法获得列数。 3.计算每个单元格的宽度。将GridView的宽度除以列数,即可得到每个单元格的宽度。 4.调用setColumnWidth()方法,将计算得到的每个单元格的宽度设置给GridView。 如果在XML布局文件中进行设置,可以在GridView标签中添加android:columnWidth属性,来指定每个单元格的宽度。例如,android:columnWidth="100dp"表示每个单元格的宽度为100dp。 需要注意的是,设置列宽时,应该考虑到屏幕的宽度以及每个单元格中的内容。如果列宽设置过小,可能会导致内容无法完全展示;如果列宽设置过大,可能会导致单元格太宽造成空白区域的浪费。因此,合理地设置列宽是很重要的。 综上所述,可以通过编程方式或者在XML布局文件中设置GridView的列宽。根据实际需要,合理地设置列宽可以使GridView的展示效果更加美观和适配不同的屏幕。 ### 回答3: GridView是一个常用的控件,用于显示数据表格。在GridView中设置列宽是非常重要的,可以使表格呈现更好的可读性和美观度。 在GridView中,可以通过以下几种方法设置列宽: 1. 使用AutoGenerateColumns属性:GridView的AutoGenerateColumns属性默认为true,可以自动根据数据源生成表格列,并根据数据的内容自动调整列宽。这种方法比较简单,但是可能无法满足个性化需求。 2. 使用列的Width属性:GridView中的各个列都是通过BoundField或TemplateField定义的。可以通过设置列的Width属性来手动控制列的宽度。例如,可以使用Width="100px"来设置某一列的宽度为100像素。 3. 使用CSS样式:可以通过为GridView的各个列定义CSS样式来设置列宽。例如,可以定义一个名为"grid-column"的CSS类,然后在GridView的列中使用CssClass属性来引用这个样式。在CSS样式中,可以使用width属性来设置列的宽度。 4. 使用ItemStyle的Width属性:GridView中的ItemStyle属性允许为每一列设置样式。可以通过设置ItemStyle的Width属性来设置列的宽度。例如,可以使用ItemStyle的Width="100px"来设置某一列的宽度为100像素。 总之,在GridView中设置列宽可以通过AutoGenerateColumns属性、列的Width属性、CSS样式和ItemStyle的Width属性等多种方式实现。根据具体需求选择适合的方法,可以使表格显示效果更好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值