如何使gridview的所有行都可编辑? [英] How to make all the row of a gridview editable?

查看:309
本文介绍了如何使gridview的所有行都可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面加载时,我需要在编辑模式下显示所有行.目前,我正在使用此代码,但是它将最后一行仅置于编辑模式.

I need to show all the row in edit mode when page load. Currently I am using this code but it putting the last row only into edit mode.

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    GridView1.EditIndex = i;
    GridView1.Rows[i].RowState = DataControlRowState.Edit;
}

我该如何克服这个问题. (我只是通过在VS 2010中将sql数据表拖到.aspx页中来创建gridview的).

How can I over come this. (I create my gridview just by dragging a sql data table into a .aspx page in VS 2010)

推荐答案

看看这些
how-to-perform-edit-在网格视图中更新和删除操作 [ how-to-editupdatedelete-gridview.aspx [全功能可编辑GridView控件 [
have a look at these
how-to-perform-edit-update-and-delete-operation-in-gridview[^]

how-to-editupdatedelete-gridview.aspx[^]

Full-featured Editable GridView Control[^]


Just将文本框添加到单元格中
Just add textbox into the cell
if (GridView1.Rows[i].Cells[j].Controls.Count == 0)
{
    TextBox tb = new TextBox();
    GridView1.Rows[i].Cells[j].Controls.Add(tb);
}


嗨...
一次查看所有行,在gridview中进行编辑.
在aspx中 :
Hi...
See for all rows edit in gridview at a time.
In aspx:
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" >
            <Columns>
                    <asp:TemplateField>
                                <HeaderTemplate>
                                                <asp:CheckBox ID = "chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" /></HeaderTemplate>
                                                            <ItemTemplate>
                                                                            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
                                                                              </ItemTemplate>
                                                                                      </asp:TemplateField>
                                                                                              <asp:TemplateField HeaderText="User Name" ItemStyle-Width = "150">
                                                                                               <ItemTemplate>
                                                                                                              <asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
                                                                                                                              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("UserName") %>' Visible="false"></asp:TextBox>
                                                                                                                                         </ItemTemplate>
                                                                                                                                                 </asp:TemplateField>
                                                                                                                                                        <asp:TemplateField HeaderText="Password" ItemStyle-Width = "150">
                                                                                                                                                                    <ItemTemplate>
                                                                                                                                                                                    <asp:Label ID = "lblCountry" runat="server" Text='<%# Eval("Password") %>'></asp:Label>
                                                                                                                                                                                                    <asp:DropDownList ID="ddlCountries" runat="server" Visible = "false">
                                                                                                                                                                                                    </asp:DropDownList>
                                                                                                                                                                    </ItemTemplate>
                                                                                                                                                  </asp:TemplateField>
                </Columns>
    </asp:GridView>


在aspx.cs中 :


In aspx.cs:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getdetails();
        }            
}
public void getdetails()
        {
            //data source=localhost;database=name;uid=root;password=*******;
            string cs = "data source=localhost;database=Murali;uid=root;password=vishwas7890;";
            MySqlConnection con = new MySqlConnection(cs);
            MySqlCommand cmd = new MySqlCommand("select * from registration", con);
            con.Open();
            cmd.ExecuteNonQuery();
            MySqlDataAdapter da=new MySqlDataAdapter(cmd);
            DataSet ds=new DataSet();
            da.Fill(ds);
            con.Close();

            gvCustomers.DataSource = ds.Tables[0];
            gvCustomers.DataBind();
        }   
<pre lang="xml">protected void OnCheckedChanged(object sender, EventArgs e)
        {
            bool isUpdateVisible = false;
            CheckBox chk = (sender as CheckBox);
            if (chk.ID == "chkAll")
            {
                foreach (GridViewRow row in gvCustomers.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
                    }
                }
            }
            CheckBox chkAll = (gvCustomers.HeaderRow.FindControl("chkAll") as CheckBox);
            chkAll.Checked = true;
            foreach (GridViewRow row in gvCustomers.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                    for (int i = 1; i < row.Cells.Count; i++)
                    {
                        row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
                        if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
                        {
                            row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
                        }
                        if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
                        {
                            row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
                        }
                        if (isChecked && !isUpdateVisible) { isUpdateVisible = true;
                        }
                        if (!isChecked)
                        {
                            chkAll.Checked = false;
                        }
                    }
                }
            }
        }



谢谢你.



Thank u.


这篇关于如何使gridview的所有行都可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆