I am using an asp gridview element to do a search on the database and return some data using
<asp:GridView ID="storedRecordsGrid" AutoGenerateSelectButton="true" emptydatatext="No data available." runat="server">
So i have a gridview that gets popolated and each row has its own select event. When the user selects a row I want to use the data in that row to populate texboxes on my page.
protected void OnRowCommand(GridViewCommandEventArgs e)
{
}
So I have this method and i have my textboxes but when the select even fires I am not sure how the data is stored for each colums of that selected row. what i want to have is something like.
protected void OnRowCommand(GridViewCommandEventArgs e)
{
mytextbox.text=datagridcolumn.value
}
I am using .net 2.0
and my c# code as of right now looks like this.
private DataTable fillGrid(string dComplainantFName,string dComplainantlName)
{
string server = "";
using (SqlConnection cnx = new SqlConnection(server))
using (SqlCommand cmd = new SqlCommand("ComplaintReportLookUp", cnx) { CommandType = CommandType.StoredProcedure })
using (DataTable dt = new DataTable())
{
cmd.Parameters.AddWithValue("@fName", dComplainantFName);
cmd.Parameters.AddWithValue("@lName", dComplainantlName);
try
{
cnx.Open();
dt.Load(cmd.ExecuteReader());
cnx.Close();
return dt;
}
catch (Exception ex)
{
throw new Exception("Error executing MyProcedureName.", ex);
}
}
}
protected void executeGrid(object sender, EventArgs e)
{
storedRecordsGrid.DataSource = this.fillGrid(dComplainantFName.Text,dComplainantLName.Text);
storedRecordsGrid.DataBind();
}
protected void OnRowCommand(GridViewCommandEventArgs e)
{
}
Thank you for your help
Thanks Tim for the help this is what ended up working for me.
protected void gridSelectedIndexChanged(Object sender, EventArgs e)
{
var row = ((GridView)sender).SelectedRow;
//var cell1Text = row.Cells[0].Text; // should be your autogenerated select cell
dComplainantFName.Text = row.Cells[1].Text.Replace(" ", ""); // Text in column 1 (the first column of your DataTable)
dComplainantLName.Text = row.Cells[2].Text.Replace(" ", ""); // Text in column 2 (the second column of your DataTable)
dComplainantMName.Text = row.Cells[3].Text.Replace(" ", "");
dComplainantAddress.Text = row.Cells[4].Text.Replace(" ", "");
dComplainantCity.Text = row.Cells[5].Text.Replace(" ", "");
dComplainantState.Text = row.Cells[6].Text.Replace(" ", "");
dComplainantZip.Text = row.Cells[7].Text.Replace(" ", "");
dComplainantPhone.Text = row.Cells[8].Text.Replace(" ", "");
storedRecordsGrid.Visible = false;
}
Use the GridView’s
SelectedIndexChangedevent, in the other events likeRowCommandorSelectedIndexChangingtheSelectedRowproperty isnull. It’s also the appropriate event for your requirement:Note: If you wouldn’t have set
AutoGenerateSelectButtonto true and have usedTemplateFields, you ought to useFindControlon theSelectedRowto get a reference to your controls.If you still insist on using
RowCommand, you could get the selected row from theCommandArgument: