Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i to all.I've a GridView with CheckBoxes in each row.Now my requirement is to update the rows that are selected by checking against their CheckBoxes in the GridView.By the way I'm not using any DataSource Controls here.Plz help me regarding this scenario.Thanks in Advance.
Posted
Comments
Sandeep Mewara 4-Jul-12 14:58pm    
By the way I'm not using any DataSource Controls here
Ok.
So, where are you storing/updating your data?

And

What have you tried so far? Any effort?
Suman Zalodiya 4-Jul-12 15:27pm    
If you are not using any data source control then how you are populating the GridView?

Any way find the below solution for your question. May be it will help you
Jagan911 5-Jul-12 0:22am    
Hi Mr.Suman thanks for the reply.Here i'm populating GridView manually by writing code in "Show Records(Button)" Event.The GridView is getting populated properly.Now,my requirement is to update multiple rows of the GridView that are selected by the CheckBoxes present in that GridView.For this i wrote the below Code.The code is running fine but only one row of data is getting updated not all the selected rows.Here i'm mentioning my code please review once so, you can suggest me a more clear solution for this.
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=hindi;Integrated Security=SSPI");
StringBuilder strSql = new StringBuilder(string.Empty);
CheckBox cc;
string s = "";

for (byte i = 0; i < GridView1.Rows.Count; i++)
{
cc = (CheckBox)GridView1.Rows[i].FindControl("c1");
if (cc.Checked)
{
s = s + GridView1.Rows[i].Cells[1].Text;
string strUpdate = " update RegistrationForm set regn_status='Y' where RegistrationID='" + s + "'";
SqlCommand cmd2 = new SqlCommand(strUpdate, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(ds);
foreach (GridViewRow row in GridView1.Rows)
{
strSql.Append(strUpdate);
}
Response.Redirect("FinalSubmit.aspx");

}
}

1 solution

Hi Jagan

Suppose ID of your CheckBox of your GridView is chkDeleteUser then use the fillowing code to delete the Rows from GridView


C#
try
        {
             //Check for CheckBox in everyrow of Gridview
            foreach (GridViewRow row in gvDeleteUser.Rows)
            {
                CheckBox chkbox = (CheckBox)row.FindControl("chkDeleteUser");
                //Check if the CheckBox for perticular row is Checked then delete it.
                if (chkbox.Checked)
                {
                    //Fetch the Primary Key value for that Row and pass to delet eit.
                    int uid = (int)gvDeleteUser.DataKeys[row.RowIndex].Value;
                    
                    //Write here logic of the delet euser
                    //For example call the stored procedure with parameter as uid
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
//once you delete the all data load gridview again to reflect the deleted users
loadGriedData();
Please don't forget to add the DataKeyName="PrimaryKeyField" property in the grid view like

<asp:gridview id="Gridview1" run="server" datakeynames="CustID" xmlns:asp="#unknown">

Please check it and let me know in case of any concerns.

And please make it as resolved if it resolves.
 
Share this answer
 

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