Obtaining values from DevExpress GridControl - c#

How to populate data into text fields whenever I click on the cell of the row?
For standard DataGridView I can use the following the code
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
txtguard_id.Text = dataGridView1.SelectedRows[0].Cells["GuardId"].Value.ToString();
DateHired.Value = (DateTime)dataGridView1.SelectedRows[0].Cells["DateHired"].Value;
Firstname.Text = dataGridView1.SelectedRows[0].Cells["FirstName"].Value.ToString();
Middlename.Text = dataGridView1.SelectedRows[0].Cells["MiddleName"].Value.ToString();
Lastname.Text = dataGridView1.SelectedRows[0].Cells["LastName"].Value.ToString();
txtguard_street.Text = dataGridView1.SelectedRows[0].Cells["Street"].Value.ToString();
txtguard_brgy.Text = dataGridView1.SelectedRows[0].Cells["Barangay"].Value.ToString();
txtguard_procity.Text = dataGridView1.SelectedRows[0].Cells["ProvinceorCity"].Value.ToString();
txtguard_age.Text = dataGridView1.SelectedRows[0].Cells["Age"].Value.ToString();
txtguard_bday.Value= (DateTime)dataGridView1.SelectedRows[0].Cells["Birthdate"].Value;
txtguard_male.Checked = (bool)dataGridView1.SelectedRows[0].Cells["Gender"].Value;
txtguard_female.Checked = (bool)dataGridView1.SelectedRows[0].Cells["Gender"].Value;
}
But in GridControl i dont know how to do it...

With DevExpress XtraGrid you can use practically the same approach:
using DevExpress.XtraGrid.Views.Grid;
//...
gridView1.RowClick += gridView_RowClick;
//...
void gridView_RowClick(object sender, RowClickEventArgs e) {
object id = ((GridView)sender).GetRowCellValue(e.RowHandle, "ID");
testBoxId.Text = id.ToString();
//...
}
or
using DevExpress.XtraGrid.Views.Grid;
//...
gridView1.RowCellClick += gridView_RowCellClick;
//...
void gridView_RowCellClick(object sender, RowCellClickEventArgs e) {
object id = ((GridView)sender).GetRowCellValue(e.RowHandle, "ID");
testBoxId.Text = id.ToString();
//...
}
Related help article: Obtaining and Setting Cell Values
UPDATE:
Note that these events will not fire when clicking on a row cell, if data editing is enabled and the ColumnViewOptionsBehavior.EditorShowMode property is set to MouseDown.
You can dissable inplace editing for view using the GridView.OptionsBehavior.Editable property.

Related

How to show grid row value in textBox on SelectionChanged event C#?

I have to show the grid selected row value into textboxes.I'm using this code, but it's not working. Any help will be appreciated.
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.SelectedRows[0].Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.SelectedRows[1].Cells[1].Value.ToString();
txtBoxName.Text = CRUD.SelectedRows[2].Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.SelectedRows[3].Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.SelectedRows[4].Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.SelectedRows[5].Cells[5].Value.ToString();
}
You're indexing your selected rows. if you have less than 6 selected rows, then you will go out of bounds. You probably want to fetch data from only one row. Check if only one row is selected and then use index 0. Make sure you set CRUD.MultiSelect = false
Alternatively use CRUD.CurrentRow which will only ever get you one row.
Form.Designer.cs:
this.CRUD.SelectionChanged += new System.EventHandler(this.CRUD_SelectionChanged);
Form.cs:
private void CRUD_SelectionChanged(object sender, EventArgs e)
{
txtBoxID.Text = CRUD.CurrentRow.Cells[0].Value.ToString();
txtBoxStates.Text = CRUD.CurrentRow.Cells[1].Value.ToString();
txtBoxName.Text = CRUD.CurrentRow.Cells[2].Value.ToString();
txtBoxAddress.Text = CRUD.CurrentRow.Cells[3].Value.ToString();
txtBoxCenter.Text = CRUD.CurrentRow.Cells[4].Value.ToString();
txtBoxCity.Text = CRUD.CurrentRow.Cells[5].Value.ToString();
}

Accept changes on datagridview as selected item in Listbox change

I'm struggling with with accepting changes to my datagridview.
I have a listbox and a datagridview. My datagridview changes based on the selected index which is selecte from the listbox. However, each time I select a different item, the datagridview items goes back to the original view/list.
My question: How can I accept/write changes back to my datatable or prevent the datagridview from refreshing everytime I select an item from the listbox?
The code for my listbox change event is:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRow[] result = ds.Tables["AssessmentItems"].Select("GroupId = " + listBox1.SelectedIndex);
var newTable = result.CopyToDataTable();
BindingSource bindSource = new BindingSource();
bindSource.DataSource = newTable;
dataGridView1.DataSource = bindSource;
}
so if you want to export your source, dont refresh it, filter it. BindingSource has a .Filter method which will help you. change your event to this. done!
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
BindingSource bindSource = new BindingSource();
bindSource.DataSource = yourOrginalSource;
dataGridView1.DataSource = bindSource;
//set your bind source filter
string myFilter = "GroupId = " + listBox1.SelectedIndex;
source.Filter = myFilter;
}
dataGridView1.databind();
needs to called for update in gridview for new changes i think.
Just set Filter property of bindingsource. But you need to declare bindingsource as you can use from any method in your class.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
bindSource.Filter = "GroupId = " + listBox1.SelectedIndex;
}

C# Binding Source Control

I am Developing a winforms application. I have two datagrid views populated from two different bindingsources(control). I am using these to implement the master detail approach. My problem is that when the first datagridview is populated using the binding source I can't select the first row of it ,because the first element in the binding source is defaultly selected and can't be selected. Can any one provide me a solution for this
As you say the first row is selected by default. So after populating the DataSource to your first GridView you can set the second GridView based on first entry. Later you check the selectionChanged Event to populate the second GridView based on selectedRow of your first one.
Code could look sth. like this:
private void PopulateDataSource()
{
dataGridView1.DataSource = myBindingSource;
DataRowView selectedRow;
if (dataGridView1.SelectedRows.Count > 0)
selectedRow = dataGridView1.SelectedRows[0] as DataRowView;
if (selectedRow != null)
dataGridView2.DataSource = myBindingSource2; //Set the BindingSource based on selectedRow in first Grid
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataRowView selectedRow;
if (dataGridView1.SelectedRows.Count > 0)
selectedRow = dataGridView1.SelectedRows[0] as DataRowView;
if (selectedRow != null)
dataGridView2.DataSource = myBindingSource2; //Set the BindingSource based on selectedRow in first Grid
}
If this doesn't work let me know but should do the job.
UDPATE
Here is a similar example using the events and methods of the bindingSource:
private void Initialize()
{
RegisterBindingSourceEvents();
dataGridView1.DataSource = bindingSource1;
dataGridView2.DataSource = bindingSource2;
bindingSource1.DataSource = myDataSource;
}
private void RegisterBindingSourceEvents()
{
bindingSource1.DataSourceChanged += BindingSource1_DataSourceChanged;
bindingSource1.CurrentChanged += BindingSource1_CurrentChanged;
}
private void BindingSource1_CurrentChanged(object sender, EventArgs e)
{
DataRowView row = bindingSource1.Current as DataRowView;
if (row != null)
bindingSource2.DataSource = myDataSource2BasedOnRow;
}
private void BindingSource1_DataSourceChanged(object sender, EventArgs e)
{
DataRowView row = bindingSource1.Current as DataRowView;
if (row != null)
bindingSource2.DataSource = myDataSource2BasedOnRow;
}
Further you maybe can use:
bindingSource.MoveNext();
bindingSource.MoveFirst();
To simulate focusing seconde row and directly first row. A bit ugly but i would guess this fires current_changed (untested). Better use first approach.
UDPATE-2
I'm sorry to tell you that this is not possible in a beautiful manner. The problem is that the Current Property of your bindingList is always set if your DataSource contains items. So if the user select the same row as the bindingSource Current Property contains your event won't get called. I found a solution which works in my example. You will need one gridEvent and maybe have to do some improvments but the idea should do the job. Sorry but without gridEvent i can't solve this:
Notice that iam using List as DataSource for my Testcase. You got DataTable and have to cast to DataRowView instead of Dummy for sure.
private bool _automatedRowChange;
private void Initialize()
{
List<Dummy> dummies = new List<Dummy> { new Dummy { Id = 1, Text = "Test1" }, new Dummy { Id = 2, Text = "Test2" } };
bindingSource1.DataSource = dummies;
dataGridView1.DataSource = bindingSource1;
//So the first row isn't focused but the bindingSource Current Property still holds the first entry
//That's why it won't fire currentChange even if you click the first row. Just looks better for the user i guess
dataGridView1.ClearSelection();
bindingSource1.CurrentChanged += BindingSource1_CurrentChanged;
dataGridView1.CellClick += DataGridView1_CellClick;
}
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var clickedRow = dataGridView1.Rows[e.RowIndex].DataBoundItem as Dummy;
var currentRow = bindingSource1.Current as Dummy;
if (clickedRow != null &&
currentRow != null &&
clickedRow.Equals(currentRow))
{
_automatedRowChange = true;
bindingSource1.MoveNext();
_automatedRowChange = false; //MovePrevious is based on the click and should load the dataSource2
bindingSource1.MovePrevious();
}
}
private void BindingSource1_CurrentChanged(object sender, EventArgs e)
{
if (!_automatedRowChange) //Check if you jump to next item automatically so you don't load dataSource2 in this case
{
//Set the second DataSource based on selectedRow
}
}

get repositoryItemGridLookupEdit parent's currentrow processed

I have a Gridview and a RepositoryItemGridLookUpEdit in that GridView
I want to show a CustomDisplayText in the RepositoryItemGridLookUpEdit
private void rgluePerson_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
var person = rgluePerson.GetRowByKeyValue(e.Value) as Person;
var name = person.Name;
var surname = person.Surname;
e.DisplayText = name + ", " + surname;
}
}
The problem is that the person name depends of another cell in the same row (in the main Gridview), and i don't know how to get the current's Gridview row being processed (current row doesn't work since i need the row being processed in the moment)...... i can't use a gridView event cuz it will change the cell value, but i want to change the Text value.
Does anyone knows how to do that?
You cannot get the row being processed by the CustomDisplayText event because there are no such fields or properties that contains current row. You can only use this event for focused row. For this your must check if sender is type of GridLookUpEdit:
private void rgluePerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
if (!(sender is GridLookUpEdit))
return;
var anotherCellValue = gridView1.GetFocusedRowCellValue("AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}
For not focused rows you can use only ColumnView.CustomColumnDisplayText event:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column.ColumnEdit != rgluePerson)
return;
var anotherCellValue = gridView1.GetListSourceRowCellValue(e.ListSourceRowIndex, "AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}

Accessing a dynamitcally added buttoncolumn's (in a Datagrid) click event. C#/ASP.NET

When I dynamically create a Datagrid and add in a new buttoncolumn how do I access the buttoncolumn_click event?
Thanks.
protected void Page_Load(object sender, EventArgs e)
{
DataGrid dg = new DataGrid();
dg.GridLines = GridLines.Both;
dg.Columns.Add(new ButtonColumn {
CommandName = "add",
HeaderText = "Event Details",
Text = "Details",
ButtonType = ButtonColumnType.PushButton
});
dg.DataSource = getDataTable();
dg.DataBind();
dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand);
pnlMain.Controls.Add(dg);
}
protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "add")
{
throw new Exception("add it!");
}
}
protected DataTable getDataTable()
{
// returns your data table
}
This article on the MSDN site clearly explains how to go about adding a button into a datagrid. Instead of using the click event of the button you'll use the command event of the DataGrid. Each button will be passing specific commandarguments that you will set.
This article shows how to use the command event with buttons. In it you use CommandArguments and CommandNames.
Here is where I create the datagrid:
System.Web.UI.WebControls.DataGrid Datagridtest = new System.Web.UI.WebControls.DataGrid();
Datagridtest.Width = 600;
Datagridtest.GridLines = GridLines.Both;
Datagridtest.CellPadding = 1;
ButtonColumn bc = new ButtonColumn();
bc.CommandName = "add";
bc.HeaderText = "Event Details";
bc.Text = "Details";
bc.ButtonType = System.Web.UI.WebControls.ButtonColumnType.PushButton;
Datagridtest.Columns.Add(bc);
PlaceHolder1.Controls.Add(Datagridtest);
Datagridtest.DataSource = dt;
Datagridtest.DataBind();
And here is the event I am trying to use:
protected void Datagridtest_ItemCommand(object source, DataGridCommandEventArgs e)
{
....
}
Thought that might help because I can't seem to capture the event at all.

Categories

Resources