Can't retrieve data out of a GridView Control
-
I’m populating a GridView control on an ASP.NET web page using a SQL Server 2005 stored procedure. The following subroutine is how I populate the GridView Control: Protected Sub Load_grdEmployees() 'Purpose: Loads the grid control grdEmployees ' with employee information 'Parameters: None 'Returns: Nothing Dim dr As SqlDataReader Dim cn As SqlConnection = New SqlConnection(MRMFunctions.WebConfigConnString) cn.Open() Try Dim cd As SqlCommand = New SqlCommand("mrmsp_EmployeeList", cn) cd.CommandType = Data.CommandType.StoredProcedure dr = cd.ExecuteReader grdEmployees.DataSource = dr grdEmployees.DataBind() Catch ex As Exception Msg = ex.Message lblErr.Visible = True lblErr.ForeColor = Drawing.Color.Red lblErr.Font.Bold = True lblErr.Text = Msg Finally cn.Close() End Try End Sub The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control. I use the ID number stored in the first column to perform edits on selected records. I’m not using a SqlDataSource control on this web page. Does anyone have any ideas on how to do this? Thank you, Quecumber256
-
I’m populating a GridView control on an ASP.NET web page using a SQL Server 2005 stored procedure. The following subroutine is how I populate the GridView Control: Protected Sub Load_grdEmployees() 'Purpose: Loads the grid control grdEmployees ' with employee information 'Parameters: None 'Returns: Nothing Dim dr As SqlDataReader Dim cn As SqlConnection = New SqlConnection(MRMFunctions.WebConfigConnString) cn.Open() Try Dim cd As SqlCommand = New SqlCommand("mrmsp_EmployeeList", cn) cd.CommandType = Data.CommandType.StoredProcedure dr = cd.ExecuteReader grdEmployees.DataSource = dr grdEmployees.DataBind() Catch ex As Exception Msg = ex.Message lblErr.Visible = True lblErr.ForeColor = Drawing.Color.Red lblErr.Font.Bold = True lblErr.Text = Msg Finally cn.Close() End Try End Sub The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control. I use the ID number stored in the first column to perform edits on selected records. I’m not using a SqlDataSource control on this web page. Does anyone have any ideas on how to do this? Thank you, Quecumber256
Quecumber256 wrote:
The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control.
+ You can get reference to the first cell of a row to get the value. + Another option is to store the key values using the DataKeyNames[^] property and you can access the value from the
DataKeys
collection. -
Quecumber256 wrote:
The problem I’m having is I don’t know how to read the data stored in the first column on the GridView Control.
+ You can get reference to the first cell of a row to get the value. + Another option is to store the key values using the DataKeyNames[^] property and you can access the value from the
DataKeys
collection.I didn't place a SqlDataSource Control on the web page. I think because I don't have this control on the page there are no references to the DataKeyNames and DataKeys properties. I spent the whole day trying to find examples on how to set these properties through code. However, everything I did find required that a SqlDataSource Control be present. My web page requires a greater amount of control than the SqlDataSource will allow. Thank you for your help, Quecumber256
-
I didn't place a SqlDataSource Control on the web page. I think because I don't have this control on the page there are no references to the DataKeyNames and DataKeys properties. I spent the whole day trying to find examples on how to set these properties through code. However, everything I did find required that a SqlDataSource Control be present. My web page requires a greater amount of control than the SqlDataSource will allow. Thank you for your help, Quecumber256
Actually, you can set the DataKeyNames property either in the control definition or in code, the values should be taken from the result data reader. For example:
GridView1.DataKeyNames = new string[] { "Id" };
....
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
object key = GridView1.DataKeys[e.NewEditIndex].Value;
}