In a form i have datagridview(book_id,book_name) and a textbox.my requirement is if i double click on book_id(cell) in dgv then i want to get quantity_available based on book_id selection
from book_stock table
book_stock (table) has columns
book_id,quantity_available
Loading
VulpesPosted May 16, 2014, 3:12 PM
private void dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == 0) // book_id column
{
object val = dataGridView1.Rows[e.RowIndex].Cells[0].Value;
if (val != null)
{
string connString = "whatever";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT quantity_available FROM book_stock WHERE book_id = @book_id", conn))
{
cmd.Parameters.Add(new SqlParameter("@book_id", val.ToString())); // assuming book_id column is character based
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read()) textBox1.Text = reader[0].ToString();
reader.Close();
}
conn.Close();
}
}
}
}
Abhishek KumarPosted May 16, 2014, 9:38 AM
Use row command get the value of bookId and by passing bookid you can get quantity.
protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName=="MyCommand")
{
int row = int.Parse(e.CommandArgument.ToString());
var cellText= gvOwner.Rows[row].Cells["bookID"].Text.Trim();
}
}
Hope this will help.