C# How to get row value of DataGridView
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
Something like this? Obviously you change the column numbers to those required.
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
test
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
Hi, something is wrong in the C# forum right now, so this message will not show as is, until things get fixed, and you won't be able to react either, you could however start a new thread if necessary. My reply is rather simple: inside the ContentClicked handler, you know the row and column index of the cell you clicked; and you can access the Rows, Columns, and Cells arrays using integer indices. So you probably want
value=dataGridView1.Rows[e.RowIndex].Cells[the_index_of_the_column_you_want];
You may need a .Value at the end. Hope this helps. :)
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
-
Hello Everyone Im generating two columns of dataGridView from database on click event button. The first column is FoodName, second column is FoodType. On my dataGridView I have set it a click event so the user can enter a number on particular cell of the third column like so...
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// make sure that the click was in the right column
if (e.ColumnIndex == 2)
{
// Value is given in case the cell is empty
string cellContent = "0";
if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
{
cellContent = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
}
using (InputBox ib = new InputBox("Enter new stock amount:", this.dataGridView1[0, e.RowIndex].Value.ToString(), cellContent))
{
if (ib.ShowDialog() == DialogResult.OK)
{
this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = ib.Result;
cellContent = ib.Result;
this.dataGridView1.Controls.Find("btn" + cellValue, true); "THIS IS THE LINE WHERE I WANT TO GET THAT cellFoodTypeValue...} } } }
How can I get the FoodType value from that row? Here is the code of my click event button... (Hence: ColFoodQtyStock and ColFoodStatus are template columns generated at run-time)
private DataGridViewTextBoxColumn ColFoodQtyStock = new DataGridViewTextBoxColumn();
private DataGridViewTextBoxColumn ColFoodStatus = new DataGridViewTextBoxColumn();private void cmdStarters\_Click(object sender, EventArgs e) { OleDbConnectionStringBuilder connBuilder = new OleDbConnectionStringBuilder(); connBuilder.DataSource = @"C:\\Users\\AP\_AE\\Desktop\\DTPOS\_APP\\DataBase\\DtposMenu.accdb"; connBuilder.Provider = "Microsoft.ACE.OLEDB.12.0"; connBuilder.Add("Jet OLEDB:Engine Type", "5"); // Food SQL Query string foodTypeSql = @"SELECT FoodName, FoodType FROM Food WHERE FoodType = @foodType"; using (OleDbConnection conn = new OleDbConnection(connBuilder.ConnectionString)) { dataGridView1.Columns.Clear();
OK, the forum is alive again, so here we go: IMO it is rather simple: inside the ContentClicked handler, you know the row and column index of the cell you clicked; and you can access the Rows, Columns, and Cells arrays using integer indices. So you probably want
value=dataGridView1.Rows[e.RowIndex].Cells[the_index_of_the_column_you_want].Value;
:)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.