Hiding/disabling ASP.NET controls when SqlDataSource is empty - c#

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.

Related

The best way to conceal DataKeyName column

There are many ways to hide a column but how do you hide one in GridView and retaic access to values in that column?
After databinding set the Column's Visible property to false.
The best way includes 3 steps.
1. Assign DataKeyName in Grid definition.
2. Set Visible to false in column definition - that will prevent sending values to the browser.
3. Use SelectedIndexChanging event of Grid view. As this event is fired long begore PageUnload the data in column still accessible. You can store that value in ViewState, Session or just pass it to another method.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
if (e.NewSelectedIndex != -1)
{
ViewState.Add("myKeyValue", GridView1.DataKeys[e.NewSelectedIndex].Value);
}

Objectlistview ModelFilter won't react

I'm trying to create some search mechanic on a ObjectListView control. According to the documentation this can be done by adding a TextMatchFilter to the control:
this.olv1.ModelFilter = TextMatchFilter.Contains(this.olv1, "search");
After executing this line, the olv1 will only show rows where the text “search” occurs in at least one cell of that row.
This is what I have done so far:
private void textBoxSearch_TextChanged(object sender, EventArgs e)
{
projectObjectListView.ModelFilter = TextMatchFilter.Contains(projectObjectListView, toolStripTextBox_search.Text);
}
Whenever I type in my textbox the TextMatchFilter is set to my ObjectListView. I have set all columns to be searchable but when I start typing, nothing happens. The event fires as it should and the Filter get set to the OLV Modelfilter, but no reaction. Anyone have experienced the same problem?
You have to set the UseFiltering property on the ObjectListView to true, otherwise the OLV will ignore any assigned ModelFilter.
To enable filtering on a list, you must set UseFiltering to true. To ensure backward compatibility, it is false by default.
Source

ASP.NET WebControls DataGrid Get Selected Row

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())

AspxGridView initially load empty

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.

DataGridView first column,first row, is selected on Load, I don't want this

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();

Categories

Resources