How to retrive a cell value from a ASPxGridView in an event? - c#

I have a simple ASPxGridView which fires a OnCustomButtonCallback
I have tried the following code:
protected void grid_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
{
MyGridView.Rows[0].Cell[0].Value;
}
But it does not see Rows: ASPxGridView does not contain a definition of Rows
I am using DevExpress. Thank you in advance.
P.S. I'd like to get data from the row in which I clicked the custom button on

You can retrieve a particular column/field values using the ASPxGridView.GetRowValues method and pass the related row's VisibleIndex retrieved from the EventArgs e.VisibleIndex property as a parameter:
protected void grid_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e) {
var rowValues = grid.GetRowValues(e.VisibleIndex, "FIELD_NAME_HERE");
}

Related

How to get asp.net dropdownlist to allow selections

I have a dropdownlist on a webform that I fill from a sql query, I then want to be able to select individual items in the dropdown and have corresponding fields from a datatable fill textboxes on the form
Problem is rowSel is returning 0 and the dropdown won't let me select any other item it always snaps back to the first itenm in the list.
Thought this might have something to do with autopostback being set to true, but if I set it to false that causes otheer problems, Not sure what else to try Im a winforms person and very new to asp.net
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
int rowSel = ddClients.SelectedIndex;
txtClient.Text = dsShow.Rows[rowSel["ClientsTableFieldA"].ToString();
}
It should allow me to select a value from the drop down then populate some textboxes with fields from the datatable.
You could try:
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Value.ToString();
}
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Text;
}
As suggested by B. Seberle
DDL items have fields value and text so it depends how you bound the ddl to SQL datasource.
place break point on txtClient.Text = ddClients.SelectedItem.Text see if item list is empty.
it should not be necessary but you can force a ddClient.databind() in page_load if(!Page.IsPostback).
however ddClients_SelectedIndexChanged will only trigger on postback.

How to get selected row index in devexpress gridcontrol?

I have devexpress gridcontrol which looks like that:
I have click event on this red X button:
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
}
How can get there row index where this button is ?
You cannot access rows on GridControl, since this is just a container for the views.
As I can see from your picture you're using GridView. When you press the delete button, focused row changes and you can access it via FocusedRowHandle.
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
var gv = myGridControl.MainView as GridView;
var index = gv.FocusedRowHandle;
gv.DeleteRow(index);
}
You can use the GridView.FocusedRowHandle property:
view.DeleteRow(view.FocusedRowHandle);

how to listen a cell of gridview if its value changed

I have a gridview and I pass It records from db and I want to change a column value when click It and save the new value into db namely update the grid how can I achieve that.Is it possible to listen a cell of gridview if its value changed or not?
Try the CellValueChanged or CellEndEdit events or you can even try the below :
private void grid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// do something with grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
}
I think you should have a saveButton and in it's click event you can save edited data to database:
private void btnSave_Click(object sender, EventArgs e)
{
DataRow[] modifiedRows = (gridView.DataSource as DataTable).Select("", "", DataViewRowState.ModifiedCurrent);
foreach (DataRow row in modifiedRows)
{ . . .}
}
You should look into CellValueChanged event which gets triggered when cell value is changed in a DataGridView. Note that this event gets triggered when the cell loses focus.
MSDN Reference

Get original object from DataRow in GridView C#

I try to display a collection (IEnumerable) of objects (generated via Linq to Sql). Therefore I bind the Gridviews DataSource property to the generated output of my Linq to SQL method. In the SelectedIndexChanged event of the GridView I try to convert the selected rows DataItem back to my original object but end up with a null value instead.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
RlDataContext dc = new RlDataContext();
this.dgvReports.DataSource = dc.GetReports(1);
this.dgvReports.DataBind();
}
protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dgvReports.SelectedIndex >= 0)
{
Report rpt = (Report)this.dgvReports.SelectedRow.DataItem;
}
}
The return type of GetReports is ISingleResult<Report>
Use a bindingsource between your datagridview and your list. When a selection is made is datagridview use the bindingsource's Current property to get you the right item from the list.
This is how I always get my information from a datagridview
string variabel = yourDataGridView["Columnname"].ToString();
You can also use this in a loop then it will be:
string variabel = yourDataGridView["RowNumber"].Cells["Columnname"].Value.ToString();
I hope this helps!

Get data from a specific column when pressing edit on that row in GridView

How do I get data from a specific column that the edit button was pressed on in a gridview?
The following code doesnt work:
protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text;
}
This is because the row is not actually selected. How can I accomplish this?
e.NewEditIndex has the row index of the currently-editing row. You can use that to access the row and read the cell data as necessary.
The parameter GridViewEditEventArgs contains the row index for the current edited row from the gridview.
You should do something like this
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = MyGridView.Rows[e.NewEditIndex].Cells[0].Text;
Another way could be implement a RowCommand event where parameter GridViewCommandEventArgs carries the command name, then you should do something like this:
void MyGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="my command name")
{
//Do stuff here
}
}

Categories