I asked this question here, too: http://www.devexpress.com/Support/Center/p/Q388764.aspx
but I hope for a quicker answer here.
I need my grid to display data only after the user starts filtering.
This is the scenario:
When opening a page containing the grid, the grid should no even try to display the data.
When the user starts searching (applies a filter), the data should be displayed.
Is this possible?
Thanks.
Ref: this
Subscribing to DataBinding event resolved all troubles with correct behavior actions like sorting, filtering and groupping events of ASPxGridView in runtime mode with requirement of getting DataTable for ASPxGridView.DataSource.
And you have answered at devexpress also it was also using the idea to implement this as i have looked by above reference link:
protected void gvData_DataBinding(object sender, EventArgs e) {
if (Convert.ToBoolean(Session["need_bind"]))
gvData.DataSource = DSource;
else
gvData.DataSource = null;
}
along this use ASPxGridView.ProcessColumnAutoFilter event handler
Do not bind the data in the Page_load.
Bind the grid to the datasource in the filter event/ search button click event..
Make the grid default to a filter that returns no records. The end user can then filter\search themselves and the ajax callbacks will repopulate the grid.
Related
In my case I use custom filter method. I have my own dropdowns and so on to choose appropriate data then I filter my dataSource call rebind and as a result I can see only filtered rows.
That works fine.
Here is the sample code:
private void BindGrid()
{
var dataSource = AllOrdersReports;
Grd.DataSource = dataSource;
Grd.Rebind();
}
The problem is that When I then push the button with code:
Grd.MasterTableView.ExportToExcel();
All records are exported to excel despite the fact that right now there are only filtered records in the grid.
Any ideas why it behaves like that ?
Ok problem solved figured ot that I had also onDataSource needed event handler implemented...
I am using a DataGrid in an ASP.NET page.
When someone clicks a row with their mouse, I want to fill in data on my form using info from that DataGrid's Selected Row.
I have wired up the control's SelectedIndexChanged event, but there does not seem to be any way to access the individual rows.
protected void DataGridRow_Selected(object sender, EventArgs e) {
var row = grid1. ???
}
Is there a way to do this?
I use Windows Forms mostly, so I could be missing something completely obvious to people who use WebForms on a more frequent basis.
You can use Ajax like JQuery to get the content of that grid cell and populate to your form field.
$("#inputfield").value($("#gridcell").html())
I need to be able to hide or disable a multitude of items (detailsviews, gridviews) when an SqlDataSource returns no rows. So if the page is reposted and no rows are selected, all the controls would be disabled.
How would I do this?
Thanks
You can try to hook to the DataBound event of GridView. If there are no rows, set Visible=false to all the controls you need to hide. Example:
<asp:gridView id="control" OnDataBound="DataBound" [...]
protected void DataBound(object sender, EventArgs e)
{
control.Visible = control.Rows.Count>0;
}
I normally use LINQ and wrap my results in a Collection or Dictionary, but as best as I can tell, you can do this by adding a new handler to the .Selected event (provided that your DataSourceMode is SqlDataSourceMode.DataSet. If you check the AffectedRows property of the SqlDataSourceStatusEventArgs in that event, it should tell you how many rows were returned (at least, the documentation implies as much).
Once you know that, you can proceed to disable or enable your other page controls.
So basically the very first column in the first row is always selected, I can't figure out a way to have it so the gridview has no selected cells. Any help?
I was having quite a bit of trouble with this myself. I have a user control with a DataGridView that is populated on application/form load. The selection of the first row seems to happen after databinding is complete and the grid is populated/rendered. The user control load event (and presumably, form load as well) fires prior to that - so calling gridView.ClearSelection() or nullifying gridView.CurrentCell in those load events has no net effect.
What finally worked for me was calling .ClearSelection() from the DataBindingComplete event of the DataGridView itself. This worked like a charm.
I had this same issue and nothing was working. The solution that worked for me was setting the 'Tabstop' property to False and calling the ClearSelection() method immediately after the data bind.
Set the DGV's CurrentCell property to null after data binding the DGV:
dataGridView1.CurrentCell = null;
Note that doing this won't prevent DGV events associated with row and cell selection from firing; you'll have to add selected row or cell count checks on RowEnter events, something like this:
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.SelectedRows.Count == 1) {
// Do stuff since a row is actually selected ...
}
}
after bounding data just call
dataGridView.ClearSelection();
I think you tried to call it before setting data to dataGrindView, if you even ever tried it
You should call: ClearSelection after event: DataBindingComplete
I had the same issue in my case, instead of set the first row visibility to false. It would be better to set the GridColor value to avoid risk on SelectionChanged Event.
Put dgv.ClearSelection() on DataBindingComplete Event and set GridColor to your DataGridView BackColor.
Set GridColor to visible color (e.g. : Gray) on your populate method / firing event.
IF I understand the Q. This prevents a cell from showing selected after data binding.
So the back color stays white. You can also Edit the columns and set it there.
DataGridView.DefaultCellStyle.SelectionBackColor = DataGridView.DefaultCellStyle.BackColor;
I also wanted read-only DataGridView, and in my case, a separate thread is slowly obtaining data and handing it to the GUI thread via a multi-thread list, and Form timer. In this approach, the GUI thread expands the data grid as needed while allowing browse.
With suggestions above the selection could be hidden, but none could prevent the cell from getting reset when my GUI thread calls dataGridView.Rows.Add() with a selection. This includes hooking events to prevent the selection, and disabling edit mode.
I found the behavior I wanted with
dataGridView.AllowUserToAddRows = false;
Now I have a dynamically sized, asynchronously loaded data grid that is read-only.
I did not like the BackgroundWorker solution, because progress is quite a burden on my loading code. Nor did I like the requirement to rebuild a new DataTable every refresh of the grid. I could not find any hints on refreshing the DataGridView with one DataTable that is being built up, but it seems like this should be possible.
Make sure your are not calling the method to load the data from the form constructor. If you call it from the form.load()
also after the datagridview is loaded do this
DataGridView.Rows[0].Selected = false;
Most of the time, it is caused by a small mistake, maybe the datagridview is set on a group box. If there are more group boxes then the selection will stop on the first group box, so keep the group box by priority basis.
I had the same problem and have solved it by overriding the OnPropertyChanged event of the GridView
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
this.ClearSelection();
}
Event _MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
GridView gv = (sender as GridView).GetDetailView(e.RowHandle, e.RelationIndex) as
GridView;
gv.ClearSelection();
I'm trying to add HyperLinkColumns dynamically to my GridView. I have the following code:
HyperLinkColumn objHC = new HyperLinkColumn();
objHC.DataNavigateUrlField = "title";
objHC.DataTextField = "Link text";
objHC.DataNavigateUrlFormatString = "id, title";
objHC.DataTextFormatString = "{2}";
GridView1.Columns.Add(objHC);
This doesn't work, so.. how can i add a HyperLinkColumn to my GridView?
You might want to add it when the row is binded:
protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
HyperLink hlControl = new HyperLink();
hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2)
hlControl.NavigateUrl = "http://www.stackoverflow.com";
e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example
}
You have to do it before the DataBinding takes place, check the GridView Events.
I think you should be using a HyperLinkField not a HyperLinkColumn.
In case, if you just want to redirect to another URL then simple use HyperLink web control and push it in the desired cell of GridView Row at RowDataBound event.
OR
If you want to perform any server event before sending it to another URL, try this
1) Add LinkButton object at RowDataBound event of GridView.
2) Set the CommandName, CommandArgument property, if requried to pass any data to this object.
3) Capture this event by handling the RowCommand event of the GridView.
By the way, I just think that you can use the DataGridView and in the Designer select the Link column and your problem would be over. The DataGridView does have a link column, than you just need to add an event on "Click" and you will be able to have what you want. This solution works if you can switch to DataGridView.
I know this thread is old but couldn't help adding my 2 cents. The procedure explained in the following tutorial worked perfectly for me:
ASP Alliance
It seems you have got things mixed up. I don't know - how that code compiles?
GridView's column collection can accept columns of type "DataControlField".
I think you need to initialize HyperLinkField and set relevant properties (text, NavigateUrl, HeaderText, Target) and add it to the columns collection.
HyperLinkColumn class is meaningful when you are using DataGrid (not in case of GridView).
Hope that helps.