Problem displaying data in gridview with object data source (L2S) - c#

I have a gridview using object datasource for data binding. Everything is working fine except, When i add some new records to data it is not displaying immediately, it requires a refresh. I am using L2S Business Object with Object Data Source. Same thing in update and delete events.

I think you miss EditIndex property, change it on every event, like :
protected void HlnkbInsert_Click(object sender, EventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
...
gv.EditIndex = -1;
DataBindGV();
}
protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
...
gv.EditIndex = e.NewSelectedIndex;
DataBindGV();
}

Are you re-binding your GridView after making the changes to your data?

Related

Why is my gridview rebound after selected index change?

I have a Gridview that is populated through SQLDataSource. The query behind is rather complex and the GridView takes some seconds to get filled; that's why I get annoyed by the fact that every time I select a row, the Gridview disappears for a while and is getting repopulated again. What does fire that rebind?
The selected row index works as Control Parameter for a second Gridview, that displays detail information on that row. There are these 2 events defined for the gridview:
protected void GridView_PURCHTABLE_OnDataBound(object sender, EventArgs e) {
if(DisplayPurchItems.Checked == false)
{
GridView_PURCHTABLE.Columns[4].Visible = false;
}
else
{
GridView_PURCHTABLE.Columns[4].Visible = true;
}
protected void GridView_PURCHTABLE_Selectedindexchanged(Object sender, EventArgs e) {
GridView_Notes.DataBind(); //this is the second gridview
}
Anyone has a clue what might cause the gridview to rebind?
Martin
Check once:Is post back.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//binding grid
}
}

Check if a value already exist in db and bound dgv before adding/saving

I have a bound datagridview and I want to check before inserting a username if that username already exists or not in my database. I insert values to dgv and database through a textBox. Thanks in advance..
private void user1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'databasDataSet.user1' table. You can move, or remove it, as needed.
this.user1TableAdapter2.Fill(this.databasDataSet.user1);
user1DataGridView.Columns[0].Visible = false;
passwordTextBox.PasswordChar ='*';
}
private void add_new_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.AddNew();
}
private void save_button_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void delete_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.RemoveCurrent();
}
To validate anything on the server-side (the existence of the username in your case) there is CustomValidator control. Add it to your page and attach it to your username TextBox. Then place your validation code to ServerValidate event handler.

GridView is hiding after Row Deleting Event

I am using GridView Row command Event for deleting row from Gridview, I have added RowDeleting Event of gridview,
but after deleting the GridView is getting hide.
Below is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack){
LoadData()//Here I am databinding the Grid
}
}
private void LoadData()
{
var data=MyClass.GetRecords();//it returns DataTable
dg.DataSource=data;
dg.DataBind();
}
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
dg.DataBind();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
dg.DataBind();
}
My guess is you are missing DataSource after deleting the rows.
Try this
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
LoadData();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
LoadData();
}
The GridView does not show any records because you do not assign a DataSource to it before calling DataBind in dg_RowDeleting.
You need to reassign the DataSource before calling DataBind anew, because it is not set again on a PostBack.
So a typical approach for the Delete part of your dg_RowCommand method would be:
Identify the row that is to be deleted. Remove the row from the database.
Reload the data from the database.
Assign the data to the DataSource property.
Bind the GridView by calling DataBind().
For steps 2-4 you will be able to call your LoadData method from dg_RowCommand. You only need to implement dg_RowDeleting if you do not implement the deletion yourself but want to do something when a row is deleted.

Pagination of GridView in ASP.NET (Won't go to page 2,3, etc.)

I have a Gridview and I want to set Pagination. So far I have:
<asp:GridView ID="grdActivity"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="30"
OnPageIndexChanging="gridView_PageIndexChanging">
C# code on the back is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataBind();
}
I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. Am I missing something? I see hundreds of pages showing this exact code to do this.
You will need to re-assign your datasource property. After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//re-assign your DataSource
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
//Set the new page index
grdActivity.PageIndex = e.NewPageIndex;
//apply your changes
grdActivity.DataBind();
}
Some notes:
you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see #ekad's answer)
if you have a lot of data you should consider paging the data directly on the database. Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid
Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like
protected void FillGrid()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
and in the page index changing event
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
FillGrid();
}
and in page load in !post back event simply call the method, like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGrid();
}
}
You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
}
private DataTable GetDataSource()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
return myDataSet.Tables["tblActivity"];
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}

Databinding action cancels selectedindexchanging event

I'm using ListView server control to represent some data. When I fire a Select command, Page does postback properly; but if i bind data source in pageload, selectedindexchanging event is being cancelled. I toggled databinding to comment in pageload and it worked properly.
Here is my databinding method.
public void BindData()
{
lstSamples.DataSource = (Session["AnalyzeApp"] as AnalizBasvurusu).SampleInfos;
lstSamples.DataKeyNames = new string[] {"Key"};
lstSamples.DataBind();
}
And i call it that way
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostback)
BindData();
}
Thanks in advance.
Call BindData() after you handle your events(SelectedIndexChanging..etc):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
BindData();
}

Categories