I created a C#.NET program to load the table to grid control. But, when I select a record in the grid control it automatically selects the first record. When I select the second time I can get the data in the correct row. I mentioned the code below.
Please help me to solve this issue;
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox4.Text = dataGridView2.SelectedCells[1].Value.ToString();
textBox3.Text = dataGridView2.SelectedCells[2].Value.ToString();
textBox11.Text = dataGridView2.SelectedCells[3].Value.ToString();
textBox1.Text = dataGridView2.SelectedCells[6].Value.ToString();
//////////////////// stock code //////////////////////////////
label14.Text = dataGridView2.SelectedCells[0].Value.ToString();
/////////////////////// category /////////////////////////////
label12.Text = dataGridView2.SelectedCells[4].Value.ToString();
//////////////////////////// purchasing price //////////////////////
label10.Text = dataGridView2.SelectedCells[7].Value.ToString();
////////////////////////// actual quantity //////////////////////////////
label11.Text = dataGridView2.SelectedCells[0].Value.ToString();
}
Try to set current cell selected explicitly
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView grid = sender as DataGridView;
grid.ClearSelection();
grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
//...rest of your code
}
Related
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;
}
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;
}
I would like to show a product balance in a TextBox when a product is selected from DataGridView.
When product is selected and the tab button is pressed then the product balance should be shown in the TextBox.
All the data is loaded from an Microsoft Access database. How can i achieve this.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(dataGridView1_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 1) //Desired Column
{
//getSum() function gives sum of a db table column
textBox1.Text = getSum();
}
}
I would add a KeyDown and a KeyUp event Handler to the DataGridView Or your Form or whatever can has focus on the time ;).
When the Key, which is pressed down is Tab i would set a member variable to true or false.
Here a short example of what i mean.
public partial class Form1 : Form
{
bool isTabPressed = false;
public Form1()
{
InitializeComponent();
button1.KeyDown += button1_KeyDown;
button1.KeyUp += button1_KeyUp;
}
void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
isTabPressed = true;
}
}
void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
isTabPressed = false;
}
}
}
Everything else you can do in your method, that you are currently writing and just check if isTabPressed is true.
Hope this helps.
If not leave a comment ;)
EDIT:
So something like this ?
Currently what it does is: you choose something and when you press tab it writes it to the textbox:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
editControlWasOpened = true;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1 != null && dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null && editControlWasOpened )
{
textBox1.Text = dataGridView1.CurrentCell.Value.ToString();
}
valueischanged = false;
}
From what i understand, based on the value you select in you ComboBox, getSum() wiill go and retrieve the total value from the database. If that's the case then add a SelectedIndexChanged event but casting the DataGridViewComboBoxCell into a ComboBox like the code below.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
var combobox = e.Control as ComboBox;
if (combobox != null)
{
combobox.SelectedIndexChanged -= ComboBox_SelectedIndexChanged;
combobox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null)
{
var item = combobox.SelectedItem.ToString();
if (!string.IsNullOrEmtpy(item))
{
textBox1.Text = getSum();
// Adjust the line above as per your requirement.
// I don't fully understand what getSum() does as you haven't posted it
}
}
}
After a long time this was done .......and i am posting this for other user too!
My Question was
"**I would like to show a product balance in a TextBox when a product is selected from DataGridView. When product is selected and the tab button is pressed then the product balance should be shown in the TextBox. All the data is loaded from an Microsoft Access database. How can i achieve this."
I think the subject is "how to show textbox value when datagridview combobox value is selected"
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
{
myCon.Open();
OleDbCommand cmd1 = new OleDbCommand("SELECT sum_stock_purchase_quantity, sum_stock_purchase_quantity_thaan from productPurchaseQuery where stock_product_id=" + this.dataGridView1.CurrentCell.Value.ToString() + "");
cmd1.Connection = myCon;
OleDbDataReader reader = null;
reader = cmd1.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox9.Text = Convert.ToString(reader["sum_stock_purchase_quantity"]);
textBox2.Text = Convert.ToString(reader["sum_stock_purchase_quantity_thaan"]);
}
}
else
{
textBox9.Text = "0.0";
textBox2.Text = "0.0";
}
myCon.Close();
Guys i did this. But this may be good or bad. But my objective is achieved.
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.
How to make column with row number? Solutions that works with default WPF dataGrid don't work with DevExpress...
You need to add a unboundcolumn to your gridview, you can do this from the designer or from code.
var col = gridView1.Columns.Add();
col.FieldName = "counter";
col.Visible = true;
col.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
gridView1.CustomUnboundColumnData += gridView1_CustomUnboundColumnData;
void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.IsGetData)
e.Value = e.ListSourceRowIndex+1;
}
set the column caption to "#"
then add this event to the gridView1
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.Caption == "#")
{
e.DisplayText = (e.RowHandle + 1).ToString();
}
}