Is there any way to refreshing a cell of a grid view without refreshing the entire grid view in c# asp.net
you could possibly use a templatefield, put an UpdatePanel in it and refresh that single cell that way. You would just need someway to initiate the refresh.
You may have to use a nested UpdatePanel within the cell.
If you don't want to put an UpdatePanel in every cell in your GridView, you can refresh a cell on a regular, full postback. The contents of the grid will be roundtripped, but your data source won't necessarily be queried in full.
First, make sure your grid doesn't rebind on every postback, e.g. wrap your gv.DataSource = x; gv.DataBind(); inside an if (!this.IsPostBack).
Then you should be able to do something along the lines of
((Label)gv.Rows[x].Cells[y].FindControl('myLabel')).Text =
GetDataItemNumber(x).FieldForColumnY.ToString();
Bear in mind that gv.Rows[x] may not correspond to the xth item in your data source, as Rows includes header rows etc. You might need to iterate through Rows checking e.g. IDs to find the correct row.
Disclaimer: I've never actually done this before, but it should be possible...
Related
After adding a row to a DataGrid, I would like to update my DataGridView (with the input source of said DataGrid) using that new row. However when I use .Update() and .Refresh(), the whole of the grid gets re-drawn. When refreshing relatively quickly (around 4 times per second), this creates an unpleasing jolty effect. I would like to find a way to add my row to the DataGridView, without re-drawing the whole thing, therefore removing the DataGridView.
C# Winforms
I think what you are looking for is a data binding.
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8
This means the DataGrid is bound to e.g. a DataTable.
So I have a grid view which gets data from a database. I want to be able to sort and page this table, as well as to remove single rows.
Now the Problem: I need to bind my data every time (also on postback), else my datasource will be null when trying to sort, but by doing so I will also get the wrong row each time I try to delete (or edit) a row.
The problem was that I thought that I had to bind the rows each time. All I had to do each time is to set the DataSouce and ONLY bind if (!IsPostBack). Then onSort i just bind again and it will be sorted. Also the delete-Function will then get the right row.
I've got an aspx with a GridView component.
My requirement is to always keep one specific row on top while sorting the others as usual, i.e. by clicking the header.
I tried with the OnSorting tag but regardless of what order I feed it into the datasource it does it's own resort anyway.
How to override this behavior, so that when the header is clicked I do the sorting in the codebehind myself, ignoring the gridview sorting?
EDIT:
GridView.DataSource = list.OrderBy(x => x.type).Select(....
GridView.DataBind();`
Initially.
Remove your special row from the list. and keep it separately.
sort the list.
bind data.
Insert your special row at 0 place of grid view
Then
DataGridView.Sorted event
Remove your special row from gridview
Insert your special row at 0 place of grid view.
Unfortunately I cannot figure out what it was. But repeated full rebuild solved this for me...
I have a couple GridViews that are dynamically created and placed into a PlaceHolder. When I mouse over the Select button, it shows __doPostBack('ctl00$bodyPlaceHolder$ctl0X','Select$Y'), where X = what I think is the GridView/Control index for the page and Y = row number of that GridView.
Since it is dynamically creating the GridViews, it makes sense that names them ctl0X, but on the PostBack how do I use this information?
I wouldn't even have this problem if adding the SelectedIndexChanged EventHandler worked, but it never gets called.
I found one other question like this, but the answer involved adding a GridView within my GridViews, which would also have to be dynamic, which brings me back to the original problem.
Edit
Okay, so I set gridViewDynamic.ID = "blahblah" + r.LastName, thus giving each GridView a unique name, so on mouseover in the page I get __doPostBack('ctl00$bodyPlaceHolder$blahblahSmith',Select$Y, I still can't access the items on PostBack because they no longer exist. So, I added the same GridView creation code to an if(IsPostBack), then called GridView gView = (GridView)this.Page.FindControl(blahblahSmith). Great, gView isn't null. But all the data in the rows are. Calling gView.Rows[0] returns null.
Use Page.FindControl("TheNameYouGaveTheDynamicGridView")
GridView grid = Page.FindControl("TheNameYouGaveTheDynamicGridView") as GridView;
If you are using MasterPages, you need to take a different approach to find the control on the page, but it is the same premise.
Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.