How to fetch column contents based on the row number (I have written the code to get the specific row number of the clicked cell.. all I want to do is retrieve all the column contents of that particular row) in data grid view using c#.
Here is my Code:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit_1);
this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;
this.comboBox1.Visible = true;
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.comboBox1.SelectedIndex = 0;
this.comboBox1.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;
this.comboBox1.Visible = true;
}
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
int i;
if (this.comboBox1.SelectedItem != null)
{
//this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
var value = (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].RowIndex.ToString());
MessageBox.Show(value); // right here i am getting the row number of clicked cell.
Assuming that you have a DataTable as the DataSource of dataGridView1
The below code gives the content of a cell if you know the row and column indices:
string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX].ItemArray[COLUMNINDEX].ToString();
The below code gives the content of a cell if you know the row index and the column name:
string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX]["COLUMNNAME"].ToString();
Related
Problem:I want to get inserted characters(AccountID) in cell of datagridview, so that i can get respective username from database.
i had stuck in problem that on keypress event of datagridview, it is not getting recently insert char. For example,if user name is 'John Snow' its ID=JN. When I write 'J' in datagridview cell, and get value on keypress, it gets "". when I write further as 'JN', it gets 'J'. similarly, on 'JNE' , it gets 'JN' , so then i get JohnSnow from Database. In short, It get previous value not recently inserted.
I have Implement it as,
public DebitCredit()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Control_KeyPress);
}
private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
//Get the column and row position of the selected cell
int column = DGV_DEBIT_CREDIT.CurrentCellAddress.X;
int row = DGV_DEBIT_CREDIT.CurrentCellAddress.Y;
DataTable result=null;
if (column == 0)
{
string test = DGV_DEBIT_CREDIT[column, row].EditedFormattedValue.ToString();
result = getpro.SearchAccountByID(test);
if (result != null)
{
if (result.Rows.Count > 0)
{
DGV_DEBIT_CREDIT[column, row].Value = result.Rows[0][0].ToString();
DGV_DEBIT_CREDIT[column + 1, row].Value = result.Rows[0][1].ToString();
}
else
{
DGV_DEBIT_CREDIT[column, row].Value = "".ToString();
DGV_DEBIT_CREDIT[column + 1, row].Value = "".ToString();
}
}
}
}
First handle EditingControlShowing event of DataGridView as shown below. For the current control getting edited in the datagridview column, handle keyUp event as shown below.
Then you can see the expected result.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyUp += new KeyEventHandler(Control_KeyUp);
}
private void Control_KeyUp(object sender, KeyEventArgs e)
{
//YOUR LOGIC
// string test = dataGridView1[0, 0].EditedFormattedValue.ToString();
}
I have a Datagridview which contains checkboxes in the first column, and text in the second.
When a Checkbox is clicked, the "CellValueChanging"-method is getting the index of the specific row.
How do I get the matching string-text? the Column-index of the text is always the same (1)
You can use GetRow() method to get row:
var row = TaskGridView.GetRow(1) as YourModel;
using ValueChangedEvent:
private void TaskGridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
if (e.RowHandle < 0)
return;
var row = TaskGridView.GetRow(e.RowHandle) as YourModel;
}
for speicific column you can do:
private void TaskGridView_CellValueChanged(object sender, CellValueChangedEventArgs e)
{
if (e.RowHandle < 0)
return;
var row = TaskGridView.GetRow(e.RowHandle) as YourModel;
if (e.Column.FieldName.Equals("YourFieldName", StringComparison.InvariantCultureIgnoreCase))
{
// do something here
}
}
Hello sir i am having trouble in focusing my datagridview i have code like this
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "2";
col.HeaderText = "2".ToString();
col.Name = "2".ToString();
dataGridView1.Columns.Add(col);
mm = e.RowIndex;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Selected=true;
}
And i want to focus this newly added column cell i have used upper code but its not working its focusing on second row because its a last column of current row i also tried this one
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if (mm != 0)
{
dataGridView1.CurrentCell = dataGridView1.Rows[mm].Cells[e.Column.Index];
dataGridView1.BeginEdit(true);
dataGridView1.Rows[mm].Cells[e.Column.Index].Selected = true;
}
}
This really confusing me i have tried all these but still its going to second row first cell what to do in this case?
[Updated]
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "";
col.HeaderText = j.ToString();
col.Name = j.ToString();
dataGridView1.Columns.Add(col);
}
And my code of tried
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if (mm != 0)
{
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = dataGridView1.Rows[mm].Cells[e.Column.Index];
dataGridView1.Rows[mm].Cells[e.Column.Index].Selected = true;
}
}
[Updated1]
The code you use is selecting cell. To select a column use this code instead:
dataGridView1.Columns[columnIndex].Selected = true;
Remember to deselect all the columns selected before if you want each column to be selected at a time. You can use some variable to save the last selected column and deselect it when needed.
UPDATE
Here is the code you should have tried:
private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
dataGridView1.ClearSelection();
e.Column.Selected = true;
}
when user edit a cell value in the event dataGridViewStudents_CellValueChanged. Data grid values are sort accordingly.
private void form_Load(object sender, EventArgs e)
{
List<student> lststudent=new List<student>();
lststudent.add("1","Abc", 26);
lststudent.add("1","xyz", 31);
lststudent.add("1","pqr", 53);
lststudent.add("1", "def", 23);
DataGridView.DataSource= lststudent;
}
private void datagridStudent_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = null;
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];
((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value = 36; ((DataGridView)sender).Sort(((DataGridView)sender).Columns["marks"], ListSortDirection.Ascending);
}
}
In this code when user edit a cell in datagrid. it does not sort the datagrid according to that column. datagrid is bind with list. so, i want to sort the datagrid when user change cell value.
private void datagridStudent_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var t = ((List<Student>)DataGridView.DataSource).OrderBy(x=>x.Marks).ToList();
//or var t =((List<Student>)DataGridView.DataSource).OrderByDescending(x=>x.Marks).ToList();
DataGridView.DataSource = t;
}
I have a DataGridView, in which I have a column. This column is not readonly, and the purpose of it is to be able to write text in every cell. When I select a empty cell and start typing, it works as expected. However, when there already is text in a cell, and I start typing, all the existing text is removed. How can I prevent this?
Example:
The cell already contains "ABC". When I type D, I want the cell to contain "ABCD", but it only contains "D".
Assume dg as your datagridview. Try assigning the following event handler EditingControlShowing, CellEnter, CellLeave to your datagridview. And you should be feeling good now.
string str = string.Empty;
int i = 0;
private void dg_CellEnter(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
str = dgv[e.ColumnIndex, e.RowIndex].Value.ToString();
}
void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
void tb_TextChanged(object sender, EventArgs e)
{
if (i == 0)
{
i++;
DataGridViewTextBoxEditingControl dgv = (DataGridViewTextBoxEditingControl)sender;
string curVal = dgv.Text;
dgv.Text = str + curVal;
dgv.SelectionStart = dgv.Text.Length;
}
dg.EditingControlShowing -= new DataGridViewEditingControlShowingEventHandler(dg_EditingControlShowing);
dg.CellEnter -= new DataGridViewCellEventHandler(dg_CellEnter);
}
private void dg_CellLeave(object sender, DataGridViewCellEventArgs e)
{
i = 0;
}
Hope it helps.