Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
for (int i = 0; i < GridView1.Rows.Count; i++)
{
    GridView1.EditIndex = i;
    GridView1.Rows[i].RowState = DataControlRowState.Edit;
}
How can I over come this. (I create my gridview just by dragging a sql data table into a .aspx page in VS 2010)
Posted
Comments
[no name] 25-Sep-13 9:26am    
r u want at a time?
sazzad37 25-Sep-13 9:31am    
yes

Just add textbox into the cell
C#
if (GridView1.Rows[i].Cells[j].Controls.Count == 0)
{
    TextBox tb = new TextBox();
    GridView1.Rows[i].Cells[j].Controls.Add(tb);
}
 
Share this answer
 
v2
Hi...
See for all rows edit in gridview at a time.
In aspx:
XML
<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>

In aspx.cs:
C#
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.
 
Share this answer
 
v2
XML
<asp:GridView ID="GridView1" runat="server" Style="z-index: 106; left: 93px; position: absolute;
 top: 283px" Width="575px" EditIndex="1" AutoGenerateColumns="False">
 <Columns>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:TextBox ID="txt1" runat="server" MaxLength="3" Style="position: static" CssClass ="gv" text = '<%# Eval("slNo") %>' AutoPostBack ="false" TabIndex ="0"  Width="25px"  Height ="15px" Visible ="true" ></asp:TextBox>
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:TextBox ID="txt2" runat="server" Style="position: static" CssClass ="gv" text = '<%# Eval("Name") %>' AutoPostBack ="false" TabIndex ="0"  Width="250px"  Height ="15px" Visible ="true" ></asp:TextBox>
 </ItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField>
 <ItemTemplate>
 <asp:TextBox ID="txt3" runat="server" Style="position: static" CssClass ="gv" text = '<%# Eval("Address") %>' AutoPostBack ="false" TabIndex ="0"  Width="150px"  Height ="15px" Visible ="true" ></asp:TextBox>
 </ItemTemplate>
 </asp:TemplateField>
</Columns>
 </asp:GridView>



and c# code

C#
protected void Button1_Click(object sender, EventArgs e)
{

for (int i = 0; i
{

GridViewRow gvr = GridView1.Rows[i];

string str1 = ((TextBox)gvr.FindControl("txt1")).Text;

string str2 = ((TextBox)gvr.FindControl("txt2")).Text;

string str3 = ((TextBox)gvr.FindControl("txt3")).Text;
//code for saving the data into database
}
}
 
Share this answer
 
C#
protected void Button1_Click(object sender, EventArgs e)
{

for (int i = 0; i
{

GridViewRow gvr = GridView1.Rows[i];

string str1 = ((TextBox)gvr.FindControl("txt1")).Text;

string str2 = ((TextBox)gvr.FindControl("txt2")).Text;

string str3 = ((TextBox)gvr.FindControl("txt3")).Text;
//code for saving the data into database
}
}
 
Share this answer
 
Comments
CHill60 1-Oct-15 7:05am    
Why have you repeated the 2nd part of a solution posted 2 years ago?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900