XtraGrid data source update while trying to edit - devexpress

I have an XtraGrid with the datasource set to a BindingList. Some of the fields in the grid are editable. The problem is that the list gets a lot of updates for some other fields (not the ones I can edit), which causes the binding to refresh. If I was in a cell part way through editing a field, this is discarded and the editor closes.
Is there a way I can make the cell with the editor open not be refreshed? Or even make that whole row not refresh if I have to?

On the grid view you can call BeginDataUpdate() to "prevent visual and internal data updates"
until EndDataUpdate() is called.
So you could do something like this (the events you attach to may be not be the best, but you get the idea):
private void gridView1_CellValueChanging(object sender, CellValueChangedEventArgs e)
{
gridView1.BeginDataUpdate();
}
private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
gridView1.EndDataUpdate();
}

Related

asp.net - RadioButton "CheckedChanged" event does not work when it is unchecked

I have two RadioButtons on my Web Form and here is the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToLongTimeString());
}
When I check RadioButton2, time appears on the page but when I check RadioButton1(RadioButton2 is unchecked now) it does not update the shown time and removes it.
the event function must be called when it checks and unchecks.I don't understand what is wrong here and why the time disappears
(As it is obvious, I know about GroupName property and AutoPostBack. My problem is something else)
You can check property of each control and check AutoPostBack=True
and also if checked="true"
I tried it on different ways and opened my pages with different browsers and I figured out a few things. the most important of them which is related to my problem:
When a control Calls back, the page refreshes and that is why the time disappears. It sends the status of all controls using post method to itself and they would be the same way as they were. But the time is not written on a control and it would not be saved.
As I said if it was for example on a Label, the text would be kept:
Label1.Text = DateTime.Now.ToLongTimeString();
It answers the first question but I still think that the event function must be called when the RadioButton is being unchecked.

Populate form with values then saved modified values with C# using asp.net webforms

My .NET skills aren't great, but I'm stumped by a little issue and I can't find anything that explains this in a way I understand. Basically I'm trying to pre-populate a form with current values from a CMS, and have those values able to be updated when the form is submitted. It's essentially just an 'edit' facility for part of a website.
I have a usercontrol which contains some form inputs like this:
<label for="">Raised by</label>
<asp:TextBox ID="RaisedBy" runat="server"></asp:TextBox>
I then have a code-behind page which pulls values from a CMS and populates this form with the values already stored for this record, like this:
protected void Page_Load(object sender, EventArgs e)
{
// ...Some logic here gets the node from the CMS and I can pull property values from it. This part works fine.
string raisedBy = node.GetProperty("raisedBy").ToString();
// Populate form input with value from CMS. This works.
RaisedBy.Text = raisedBy;
}
So this is fine. But when the form is submitted it calls the following code:
protected void edit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// Get edited value of input field
string RaisedByVal = RaisedBy.Text;
// Do some logic here to set up the CMS to be able to save the property - this works although it uses the original value of the form not the modified value if the user has changed it.
pageToEdit.getProperty("raisedBy").Value = RaisedByVal;
}
}
The problem is that the original form values are being saved back to the system rather than the modified values if the user has edited them.
Can anyone suggest what I'm doing wrong and how to get the modified values to be used rather than the original values?
Many thanks.
You have to check whether it is Postback or not in Page_Load() method:
So if you don't do this then on edit button click it will 1st call the Page_Load() and will again reset the original value. Later it will call the Edit click method and you will still see the original data.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// ...Some logic here gets the node from the CMS and I can pull
property values from it. This part works fine.
string raisedBy = node.GetProperty("raisedBy").ToString();
// Populate form input with value from CMS. This works.
RaisedBy.Text = raisedBy;
}
}
Typically I found the answer right after posting! :)
I needed to wrap the 'pre-populate form values' logic inside a:
if (!IsPostBack)
{
}
block.

Due to changing the order of statements two postbacks are required instead of just one, before …

If we have the following code, then when user clicks an Edit button, page is posted back and put into Edit mode:
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwEmployees.EditIndex = e.NewEditIndex;
gvwEmployees.DataSource = ds.Tables["Employees"];
gvwEmployees.DataBind();
}
But with the following code, user has to click the Edit button twice before a row is put into edit mode ( thus page needs to be posted back twice before row gets into edit mode). Why does it matter whether gvwEmployees.EditIndex is assigned a value before or after we bind GridView to a data source?
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvwEmployees.DataSource = ds.Tables["Employees"];
gvwEmployees.DataBind();
gvwEmployees.EditIndex = e.NewEditIndex;
}
Thank you
Modifying the EditIndex property with a value different than the one it already has requires that DataBind() is called after the modification.
As described in the GridView.EditIndex documentation page, it could also happen if EditIndex is modified under other circumstances:
If you set the EditIndex property
after a postback or in handlers for
events that are raised later than the
Load event, the GridView control might
not enter edit mode for the specified
row. If you read the value of this
property in other event handlers, the
index is not guaranteed to reflect the
row that is being edited.

When GRidView's row is in edit mode, textboxes don't display current values

1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}

Postback destroys user controls in my GridView columns

I have a ASP.NET GridView that uses template columns and user controls to allow me to dynamically construct the datagrid. Now I'm implementing the event handler for inserting a row. To do that, I create an array of default values and add it to the data table which is acting as a data source. However, when my OnLoad event is fired on postback, all my template columns no longer have the user controls. My gridview ends up just being all blank with nothing in it and my button column disappears as well (which contains the add row, delete row and save buttons).
My row add event just does this:
public void AddDataGridRow()
{
List<object> defRow = new List<object>();
for (int i = 0; i < fieldNames.Count; i++)
{
defRow.Add(GetDefaultValueFromDBType(types[i]));
}
dt.Rows.Add(defRow);
}
It is fired from a button in a user control that's implement like this:
protected void Button1_Click(object sender, EventArgs e)
{
((Scoresheet)(this.Page)).AddDataGridRow();
}
My on load event does a bunch of stuff on first run to set the GridView up but I don't run that again by using the IsPostBack property to tell.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Initialize();
}
Anyone have any hints as to why my user controls are vanishing?
You have to add the controls to the grid on every page_load, not just if it's (!Postback)
Do you have the EnableViewState=true on the usercontrols and the GridView?
Is the AddDataGridRow() method called by Initialize()? You basically have two options:
Bind the grid on every postback and do not use viewstate (performace loss)
Bind the Grid only the first time (if (!IsPostBack)), and make sure that your user controls keep their viewstate.
From your code, it is not clear whether the user controls keep viewstate and what they have in them. It is not even clear what is the execution order of the methods you've shown. There is no binding logic, so even if you keep adding rows, the grid may still not be bound. Please elaborate a bit and show the whole page codebehind.

Resources