My problem is that it will only display the contents of datagrid on textboxes when i click on the cells under Price column which has a Money DataType all others except for ItemNo is Varchar(100). Please help, thanks
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView1.CurrentRow;
txtDsc.Text = row.Cells["Description"].Value.ToString();
txtQty.Text = row.Cells["Qty"].Value.ToString();
txtUnt.Text = row.Cells["Unit"].Value.ToString();
txtPrc.Text = row.Cells["Price"].Value.ToString();
txtRmr.Text = row.Cells["Remarks"].Value.ToString();
}
As the other answer pointed out the code seems to be working fine.
I suspect the event is not firing off. Try putting in a breakpoint and investigate that bit.
I am presuming you need CellClick instead of CellContentClick.
Refer CellContentClick event doesn't always work
Alternatively if you are trying to display the text box values based on the selection on the data grid view, use DataGridView.SelectionChanged Event
I'm not entirely sure why it is only populating when you click that cell; I recreated your code and it works just fine. I did manage to recreate it again using the following code:
private void myDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int row = myDGV.CurrentCell.RowIndex;
txtDsc.Text = myDGV.Row[row].Cells["Description"].Value.ToString();
txtQty.Text = myDGV.Row[row].Cells["Qty"].Value.ToString();
txtUnt.Text = myDGV.Row[row].Cells["Unit"].Value.ToString();
txtPrc.Text = myDGV.Row[row].Cells["Price"].Value.ToString();
txtRmr.Text = myDGV.Row[row].Cells["Remarks"].Value.ToString();
}
Related
I want to store on a variable the value of the first column of a row whenever I click on a cell.
You can try this code :
private void MyDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
var val = MyDataGrid.Rows[e.RowIndex].Cells[0].Value;
}
Hope this help you.
i have an combobox cell and i wanna convert to textbox cell after select item from the same combobox cell and convert to textbox but the problem when using
[1, DataGridView1.CurrentRow.Index] = new DataGridViewTextBoxCell()`
the cell not change until click on another cell although i'm using Datagridview1.refresh()
the first picture (when select from combobox then it should be changed )
the second picture ( after click an another cell )
DataGridViewComboBoxCells are notoriously obtuse when it comes to such a simple thing as simply and fully accepting a selected value..
This seems to work for me:
ComboBox editComboBox = null;
private void DataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox && e.ColumnIndex == 1)
{
editComboBox = (ComboBox)e.Control;
editComboBox.SelectionChangeCommitted -= editComboBox_SelectionChangeCommitted;
editComboBox.SelectionChangeCommitted += editComboBox_SelectionChangeCommitted;
}
}
void editComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
DataGridView1[1, dataGridView5.CurrentRow.Index] = new DataGridViewTextBoxCell();
DataGridView1[1, dataGridView5.CurrentRow.Index].Value =
editComboBox.SelectedItem.ToString();
DataGridView1.EndEdit();
DataGridView1[1, dataGridView5.CurrentRow.Index]cell1.Selected = false;
editComboBox = null;
}
Note that calling EndEdit allegedly can raise an exception if no error handler is found. I don't have one and I still don't get an error. Not sure what the docs mean; maybe that it may raise an error if the data are in fact invalid..
Also note that with this design the user can not easily correct an edit, unless you provide a way to restore the dropdown..
I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}
I have DataGridView to load some data programmatically. After inserting my data I am showing the DataGridView . Here by default the 1st row 0th column cell is selected. But I don't need that. I have tried to disable that option.
datagridviewname.currentcell=null
But it will not work. Any body can help me to solve my problem.
Set CurrentCell Selected property to False like:
dataGridViewName.CurrentCell.Selected = false;
On the DataGridView create an event for DataBindingComplete then add this method datagridview1.ClearSelection()
private void datagridview1_DataBindingComplete(object sender, EventArgs e)
{
datagridview1.ClearSelection();
}
Why u set it null? It should be like following. I think it will work
dataGridViewName.Rows[0].Cells[0].Selected = false;
if it is 1st row 0th, then
dataGridViewName.Rows[1].Cells[0].Selected = false;
The only way is this:
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
dataGridView.ClearSelection();
}
You should perform clear selection inside form load event of the form instead of constructor.
private void Form1_Load(object sender, EventArgs e)
{
// You will get selectedCells count 1 here
DataGridViewSelectedCellCollection selectedCells = dataGridView.SelectedCells;
// Call clearSelection
dataGridView.ClearSelection();
// Now You will get selectedCells count 0 here
selectedCells = dataGridViewSchedule.SelectedCells;
}
MSDN
You can set this property to null to temporarily remove the focus rectangle, but when the control receives focus and the value of this property is null, it is automatically set to the value of the FirstDisplayedCell property.
So looks like setting it to null only works if it wasn't the first row first column cell.
Consider the following picture
I get the selected row values in the three textboxes shown in the figure when i click a cell using following code.
void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e) {
TBGRNo.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
TBSName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
TBFName.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
}
My Question is: how will I do the same thing in DevExpress XtraGrid control??
Here is the way that I've followed,
int[] selRows = ((GridView)gridControl1.MainView).GetSelectedRows();
DataRowView selRow = (DataRowView)(((GridView)gridControl1.MainView).GetRow(selRows[0]));
txtName.Text = selRow["name"].ToString();
Also you can iterate through selected rows using the selRows array. Here the code describes how to get data only from first selected row. You can insert these code lines to click event of the grid.
You can do this in a number of ways. You can use databinding (typical initialized after InitializeComponent();)
textBox1.DataBindings.Add(new Binding("Text", yourBindingSource,
"TableName.ColumnName", true, DataSourceUpdateMode.OnPropertyChanged));
or use a DataLayoutControl (if you are going to use textbox for editing, I really recommend spending some time to learn how to use this component.
or in FocusedRowChanged by assigning from one of these methods:
textBox1.Text = gridView1.GetDataRow(e.FocusedRowHandle)["Name"].ToString();
textBox1.Text = gridView1.GetFocusedDataRow()["Name"].ToString();
textBox1.Text = (gridView1.GetFocusedRow() as DataRowView).Row["Name"].ToString();
textBox1.Text = gridView1.GetFocusedRowCellValue("Name").ToString();
I found the solution as follows:
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
{
TBGRNo.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "GRNo").ToString();
TBSName.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SName").ToString();
TBFName.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FName").ToString();
}
Which one of their Grids are you using? XtraGrid or AspXGrid? Here is a piece taken from one of my app using XtraGrid.
private void grdContactsView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
_selectedContact = GetSelectedRow((DevExpress.XtraGrid.Views.Grid.GridView)sender);
}
private Contact GetSelectedRow(DevExpress.XtraGrid.Views.Grid.GridView view)
{
return (Contact)view.GetRow(view.FocusedRowHandle);
}
My Grid have a list of Contact objects bound to it. Every time a row is clicked I load the selected row into _selectedContact. Hope this helps. You will find lots of information on using their controls buy visiting their support and documentation sites.
For VB.Net
CType(GridControl1.MainView, GridView).GetFocusedRow()
For C#
((GridView)gridControl1.MainView).GetFocusedRow();
example bind data by linq so use
Dim selRow As CUSTOMER = CType(GridControl1.MainView, GridView).GetFocusedRow()
All you have to do is use the GetFocusedRowCellValue method of the gridView control and put it into the RowClick event.
For example:
private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
{
if (this.gvCodigoNombres.GetFocusedRowCellValue("EMP_dni") == null)
return;
MessageBox.Show(""+this.gvCodigoNombres.GetFocusedRowCellValue("EMP_dni").ToString());
}
var rowHandle = gridView.FocusedRowHandle;
var obj = gridView.GetRowCellValue(rowHandle, "FieldName");
//For example
int val= Convert.ToInt32(gridView.GetRowCellValue(rowHandle, "FieldName"));