Mayank
Mayank

Reputation: 41

How to update multiple rows using gridview in ASP.net on a SINGLE BUTTON CLICK?

The functional requirement is to fetch the list of students (3 coloums) from database (SQL Server) and display it on the web page, along with a blank field in front of each row for entering data. Next, to allow the user to enter marks scored by students in the test and update those in the database.

Now, I know this can be done using gridview by having an Update Button Field as separate coloumn. But in that case there would be an update button in front of each row and user would need to click it for each student (more than 100). This is a tedious task for user.

I want that user enters the marks for all the students and then click only 1 button, which would update all the rows.

On button click event we can use foreach loop for GridViewRows, but please help me with user interface. How to make it possible?? How to use single button click instead of 'n' clicks??

Can it be done using gridview? Or is there something else which can accomplish the task??

Thanks

Upvotes: 4

Views: 20735

Answers (3)

Turing
Turing

Reputation: 114

Although very late, but may be someone my look for it. Since you did not mentioned any condition or example of code, so I assume this, below is the code:

private void UpdateAllRecord()
    {
        StringBuilder query = new StringBuilder();
        for (int i = 0; i < GridViewName.Rows.Count; i++)
        {
            GridViewRow row = GridViewName.Rows[i];
            using (SqlConnection con = new SqlConnection(connStr)) //use your connection string
            {
                con.Open();
                SqlCommand cmd1 = new SqlCommand("update YourTable set  ColumnName=@ColumnName where Id= " + row.Cells[0].Controls.OfType<TextBox>().FirstOrDefault().Text + "  ", con);
                cmd1.Parameters.AddWithValue("@ColumnName", row.Cells[5].Controls.OfType<TextBox>().FirstOrDefault().Text);                
                cmd1.ExecuteNonQuery();
                con.Close();
            }
        }
    }

// Now call the UpdateAllRecord() to your button click event or any other event

protected void upload_Click(object sender, EventArgs e)
    {
        UpdateAllRecord()
    }

Upvotes: 0

Niels Ziegler
Niels Ziegler

Reputation: 456

A complete solution for multi line editing can be found at Matt Dotsons Blog. I use it in my own application.

What you need to do at minimum, besides registering the new gridview type and exchanging your existing asp:GridView with the new type, is to tell the GridView the ID of your "SaveButton". This button will then trigger the RowUpdating/Updated events for each row that changed.

Please refer to Dotsons Blog post for details and the downloadable sourcecode.

Upvotes: 0

Waqas Raja
Waqas Raja

Reputation: 10870

place a textbox inside template field in fourth column

<asp:TemplateField HeaderText="Marks Scored">
    <ItemTemplate>
        <asp:TextBox runat="server" ID="txtMarksScored" />
    </ItemTemplate>
</asp:TemplateField>

Then inside button click event loop through grid view rows and get the textbox to get the value entered.

protected void Submit_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        TextBox txtMarksScored = (TextBox)gvr.FindControl("txtMarksScored");
        // Hope you understand what to do next?
        // txtMarksScored.Text
    }
}

Upvotes: 7

Related Questions