RG-3
RG-3

Reputation: 6188

GridView is not refreshing when adding a row: Tried Databind()

I have a GridView in my page but I am NOT adding my row through GV. Adding thru textboxes and then displaying it to the GV. Now the problem is that, when I hit ADD button my GV is not showing that updated row. If I click that page on my menu bar, then my whole page refreshes and I can see my updated GV.

Here is the mock up of my ADD_Click() event:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
                        var td = from s in cv.Test1
                                 join r in dt.Test2 on s.ID equals r.ID
                                 where s.Col1 == ColumnName
                                 select s;

                        gvShowComm.DataSource = td;
                        gvShowComm.DataBind();

                    }

                    catch (Exception err)
                    {
                        //Nothing!!!
                    }
            }

            else
            {
                MessageBox.Show("Please Enter Entry for the textboxes!");
            }
        }

Now when I am adding the records, GridView is not showing the updated record. Can anyone tell me whats going on?

Thanks!

Upvotes: 1

Views: 549

Answers (1)

Kelsey
Kelsey

Reputation: 47766

First thing you should verify is that your td object has the correct amount of rows you are expecting. The GridView should be binding correctly to that object so my hunch is your result in td is missing the row.

How are you 'adding' the row? Are you saving it to the DB first and then running your linq call? From the above code it just looks like your calling some linq to fetch a result but there is nothing showing you adding anything that would cause the result to be different that the initial load.

You add pseudo code should be like:

  1. Save the new row to the database
  2. Fetch the new data
  3. Bind the new data to the grid

You could also do it manually by fetching the old data. Adding a row manually to the old data (stored in td in your example) before binding it and then do the binding after you have manually added a row to the linq result.

Upvotes: 2

Related Questions