我有一个gridview,但是上面没还有添加编译按钮,希望用户一进入页面就直接进入编辑状态,应该如何实现

解决方案 »

  1.   

    其实编辑按钮就是设置EditIndex你想编辑那一行,就设置index为多少if(!IsPostBack)
    {
     GridView1.EditIndex = ?;
    }
      

  2.   

    to dengchenlu:
    我有4个字段,其中有2各需要编辑,没有多余的按钮,希望用户进入页面以后直接就可以对所有行中的那2列进行编辑。
      

  3.   

    把那两列的itemtemplate直接显示成文本框 ,其他的列用label
      

  4.   


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
    GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
    OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Wno" HeaderText="Wno" ReadOnly="True" />
                    <asp:BoundField DataField="Cname" HeaderText="Cname" />
                    <asp:BoundField DataField="Fname" HeaderText="Fname" />
                    <asp:BoundField DataField="Matter" HeaderText="Matter" />
                    <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                    <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                    <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                </Columns>
            </asp:GridView>
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        private void bind()
        {
            string sql = "select Wno,Cname,Fname,Matter from Cuser";
            DataSet ds = new DataSet();
            ds = Conn.getDataSet(sql);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataKeyNames = new string[] { "Wno" };
            GridView1.DataBind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from Cuser where Wno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            Conn.executeDML(sqlstr);
            bind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string sqlstr = "update Cuser set Cname='"
                + ((DropDownList)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Fname='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Matter='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "'where Wno='"
                + GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim() + "'";
            Conn.executeDML(sqlstr);
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')");
                }
            } 
        }可以参考下,有什么不对的再改改,没测试