Refresh Master-Detail gridcontrol - devexpress

I have a devexpress Master-View gridcontrol , bound to Entity bindingsource.
Programatically I update some records on bindingsource , but the Gridcontrol is not refreshed with new data.
I use the following methods : ( GridviewMaster - is Master View , GridviewChild - is Detail view )
GridControl1.RefreshDataSource()
GridControl1.MainView.RefreshData()
GridControl1.DefaultView.RefreshData()
GridViewMaster.RefreshData()
GridViewChild.RefreshData()
But without success.I know that the bindingsource is updated , but the Gridcontrol is not. Only when i close and open the form again the gridcontrol is updated.
What can I do ? ( without querying again the database)
Thank you !
'

Have you tried rebinding it to the datasource?
GridViewMaster.DataSource = DataSource;
GridViewMaster.DataBind();
This reloads the table with the new data.

Stumbled upon the same problem and the only way I came up with to force refresh the affected rows was to collapse and reexpand them (if they are expanded in the first place - collapsed rows don't need this sort of thing):
var rowsWhichNeedToBeRefreshed = ...; // find the master rows which are expanded using e.g. GetMasterRowExpanded()
if (rowsWhichNeedToBeRefreshed.Any())
{
try
{
gridcontrol.SuspendLayout();
rowsWhichNeedToBeRefreshed.ForEach(x =>
{
gridview.CollapseMasterRow(x.Item1);
gridview.SetMasterRowExpanded(x.Item1, 0, expand: true);
});
}
finally
{
gridcontrol.ResumeLayout(performLayout: true);
}
}

Related

How can I clear a datatable in asp listview?

When I use the code below, I remove the datatable values, but the data table structure still exists and displays empty fields (see pics) with the DOM explorer showing an empty table and table rows.
How can I clear the datatable values and the table itself? This way when I repopulate search again, the empty smaller table isn't present?
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
Before
After items.clear and datasource = null
This is ridiculous and I believe there is a better way to do this, but the never ending server/client battle makes this harder than it should be. My listview binded to a datatable is called lvwOutput.
In my btnClear I had to put the following. You cannot hide the element or clear the items in the server side asp code for this to work
ScriptManager.RegisterStartupScript(Page, GetType(), "emptyTable", "javascript:emptyTableRows(); ", true);
In my javascript code I had to put the following, this clears the client code
function emptyTableRows(){
var tableHeaderRowCount = 0;
var table = document.getElementById('lvwOutputTable');
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
}
And then in the portion of my code that would display the listview and datatable when the user initiates another sql search. This clears the server side.
lvwOutput.Items.Clear();
lvwOutput.DataSource = null;
lvwOutput.DataBind();
You can create a property the stores the data table in session that way you can access it during the click event.
DataTable dtbleDataSource
{
get
{
return Session["dataSource"] as DataTable
}
set
{
Session["dataSource"] = value;
}
}
In your click event you can say:
dtbleDataSource.Reset();

How do I programatically clear the HasSelection property of the WebGrid?

Self explanatory question.
Between posts, the grid I have setup is retaining the HasSelection bit, even if the WebGrid has been re-loaded with new data. Therefore, the functionality I have wired into the physical selection of a WebGrid record runs, even though the user hasn't selected anything on the new resultset yet.
Thoughts?
WebGrid obtains the selected row thru the query string. by default, the query string field is row like http://localhost/grid?row=2
Ideally, you would remove that query string field before posting back like so http://localhost/grid
If it is not possible, set WebGrid.SelectedIndex to -1 instead.
Edit
Here are few ways to set WebGrid.SelectedIndex:
#{
WebGrid grid = new WebGrid(Model);
grid.SelectedIndex = ViewBag.SelectedIndex;
#grid.GetHtml(
columns: grid.Columns(
...
)
)
}
public ActionResult Index(int? row)
{
ViewBag.SelectedIndex = (IsKeepSelection() ? row.GetValueOrDefault() : 0) - 1; //TODO: add bound checking
return View(People.GetPeople());
}
Or (I prefer the previous one though since it's easier to understand):
#{
WebGrid grid = new WebGrid(Model);
if(ViewBag.ClearSelection) {
grid.SelectedIndex = -1;
}
#grid.GetHtml(
columns: grid.Columns(
...
)
)
}
public ActionResult Index(int? row)
{
ViewBag.ClearSelection = IsClearSelection();
return View(People.GetPeople());
}
The ultimate answer to this issue was to clear out the "action" on the form. Apparently, the WebGrid is tightly coupled to this value and therefore the makers of the WebGrid expected you to futz with the action attribute instead of the WebGrid itself.
I simply reset the querystring with document.forms[0].action = window.location.pathname; in my submission button. Since the form's action resolves to the querystring, this fixed it.

How do I Reset the Values in My ASP.NET Fields?

The current form is here. It is not complete, and only a couple options will work.
Select "Image CD" and then any resolution and click "Add to Order." The order will be recorded on the server-side, but on the client-side I need to reset the product drop-down to "{select}" so that the user will know that they need to select another product. This is consistant with the idea that the sub-selections disappear.
I don't know whether I should be using ASP postback or standard form submittal, and most of the fields need to be reset when the user adds an item to the order.
I'd use Response.Redirect(Request.RawUrl) method to reset the form data from the server side.
A little explanation: this is PRG design pattern
Used to help avoid certain duplicate
form submissions and allow user agents
to behave more intuitively with
bookmarks and the refresh button.
A workaround for this question:
necessary data may be stored in Session for example. This means we getting the data with the first POST, putting it to the storage, performing redirect and getting it back.
In the pageload event on the form, you need to add something simalar to this:
if (IsPostBack)
{
//Proccess the order here
ProductOption.SelectedIndex = 0;
}
That will allow you to process the order, but then start over the order form.
The simplest means would be a recursive function that forks on the type of control
private void ResetControls( Control control )
{
if ( control == null)
return;
var textbox = control As TextBox;
if ( textbox != null )
textbox.Text = string.Empty;
var dropdownlist = control as DropDownList;
if ( dropdownlist != null )
dropdownlist.SelectedIndex = 0; // or -1
...
foreach( var childControl in controlControls )
ResetControls( childControl );
}
You would this call this function in your Load event by passing this. (This is presuming you want to reset more than a single control or small list of controls).

asp.net use details view if 1 record is returned and gridview if more than one record

I'm using a details view and a sqldatasource control to populate it. Every once in a while i get an error message because more than one row is returned. How can I have the data display in a gridview instead if more than one row is returned?
Databind to both and put this in the OnDataBound event or wherever appropriate in your code. (Obviously you'll need to tweak the code for the names of your objects)
if(myDataTable.Rows.Count > 1)
{
myGridView.Visible = true;
myDetailsView.Visible = false;
}
else
{
myGridView.Visible = false;
myDetailsView.Visible = true;
}

How do I count checked checkboxes across all pages of a gridview using jquery?

I want to instantly update a status line indicating the number of checked checkboxes across all pages of an asp.net gridview. Right now I am only ably to count the number of checkboxes that are checked on the current gridview page.
Here is my code:
$(document).ready(initAll);
function initAll(){
countChecked();
$(".activeBoxes").click(countChecked);
}
function countChecked() {
var n = $(".activeBoxes input:checked").length;
$("#checkboxStatus").text(n + (n == 1 ? " vehicle is" : " vehicles are") + " selected on this page. ");
if( n == 0){
$(".activateButton").hide();
$("#checkboxStatus").hide();
}else{
$("#checkboxStatus").show();
$(".activateButton").show();
}
}
Keep a hidden text field on your page and everytime you check a box, call a javascript method that will write the 'id' of the checkbox to the hidden field. Each time you postback your page, serialise the hidden field's value to the session in your desired objects structure (be it objects, hash table, array etc).
Upon rendering the page, each checkbox can check the session object structure (that you have created before) and determine if the state of the checkbox was last checked or not.
You could use JQuery to loop through all checkboxes on the page and increment a counter if the checkbox is checked.
You can track the total selection in viewstate (or something similar) on page change. I did something similar tracking the selected row ID's in an array. In my case I had to re-check the items when they returned to the page. Additionally if you allow sorting the selection may move across pages.
Edit: Sorry this doesn't actually your Jquery question, but maybe it will help...
What you're missing is removing the ID. When checking rows and tempid is not checked make sure it is not in saveids.
Do you know that Google is your friend?
Selecting CheckBoxes Inside GridView Using JQuery
The WML Video
And without JQuery but for more perfectionist behavior (like the image below), try this link
alt text http://www.gridviewguy.com/ArticleImages/GridViewCheckBoxTwistAni.gif
I am using a viewstate to keep track of all checked items across all pages and rechecking them upon returning to the page.
I will have to add my viewstate value to the page total and somehow subtract overlapping totals. Since my jquery does not include an id, this will be tricky.
protected ArrayList savedIds;
if (ViewState["SavedIds"] == null) savedIds = new ArrayList();
else savedIds = (ArrayList)ViewState["SavedIds"];
List<int> activateList = new List<int>();
foreach (GridViewRow tt in GridView2.Rows)
{
CheckBox cb = (CheckBox)tt.FindControl("ActivateItem");
HiddenField id = (HiddenField)tt.FindControl("IdField");
if (cb.Checked)
{
int tempId = 0;
string tempId2 = id.Value.ToString();
int.TryParse(tempId2, out tempId);
activateList.Add(tempId);
}
}
foreach (int activateId in activateList)
{
if (!savedIds.Contains(activateId.ToString())) savedIds.Add(activateId.ToString());
}
ViewState["SavedIds"] = savedIds;

Resources