Problem reading a data value from my DataGrid
-
I'm a newbie so please help me here. I've got a project due Tuesday that I'm working on for college and this part is kicking my rear. I'm developing a WebForm in Visual studio. this WebForm shows my DataGrid fine and I added a Select button for column(0) while using property builder and converted to a template column. What I want it to do is select the row of data and read the data values from the selected row and put into textboxes outside the DataGrid on the form. I will use these textbox values to use in my stored procedures to update multiple tables with data. do i have to change any HTML on the button I make to retreive this data that is selected in my datagrid? As it stands I can select the grid row and it highlights but when I hit the button to get the data nothing happens. I'm not sure how to call this sub sub GetRowReqID(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Dim MyData As String Dim MyTB as TextBox MyTB = CType(e.Item.Cells(1).FindControl("RequestID"), Textbox) MyData = MyTB.Text txtReqID.Text = MyData //Textbox on form where I want the value of //RequestID to be copied. End Sub how do I call this sub to execute from my button?
-
I'm a newbie so please help me here. I've got a project due Tuesday that I'm working on for college and this part is kicking my rear. I'm developing a WebForm in Visual studio. this WebForm shows my DataGrid fine and I added a Select button for column(0) while using property builder and converted to a template column. What I want it to do is select the row of data and read the data values from the selected row and put into textboxes outside the DataGrid on the form. I will use these textbox values to use in my stored procedures to update multiple tables with data. do i have to change any HTML on the button I make to retreive this data that is selected in my datagrid? As it stands I can select the grid row and it highlights but when I hit the button to get the data nothing happens. I'm not sure how to call this sub sub GetRowReqID(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Dim MyData As String Dim MyTB as TextBox MyTB = CType(e.Item.Cells(1).FindControl("RequestID"), Textbox) MyData = MyTB.Text txtReqID.Text = MyData //Textbox on form where I want the value of //RequestID to be copied. End Sub how do I call this sub to execute from my button?
Hi there. When some controls (like a
Button
) in a DataGrid triggers an event, the DataGrid fires itsItemCommand
event. TheDataGridCommandEventArgs
will include theCommandName
of the control that triggered the event - in this case, the CommandName would be "Select". So in your code you could test for the CommandName of "Select", and execute the rest accordingly. To establish a handler for theItemCommand
event in Visual Studio, select the grid and look at the Properties window for it. Click the Events button in the Properties window, and double-click onItemCommand
. Visual Studio will create the sub stub for you, then you can use anIf
orSelect Case
statement to test for "Select" as theCommandName
, and execute yourGetRowReqID
procedure as necessary. -
I'm a newbie so please help me here. I've got a project due Tuesday that I'm working on for college and this part is kicking my rear. I'm developing a WebForm in Visual studio. this WebForm shows my DataGrid fine and I added a Select button for column(0) while using property builder and converted to a template column. What I want it to do is select the row of data and read the data values from the selected row and put into textboxes outside the DataGrid on the form. I will use these textbox values to use in my stored procedures to update multiple tables with data. do i have to change any HTML on the button I make to retreive this data that is selected in my datagrid? As it stands I can select the grid row and it highlights but when I hit the button to get the data nothing happens. I'm not sure how to call this sub sub GetRowReqID(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Dim MyData As String Dim MyTB as TextBox MyTB = CType(e.Item.Cells(1).FindControl("RequestID"), Textbox) MyData = MyTB.Text txtReqID.Text = MyData //Textbox on form where I want the value of //RequestID to be copied. End Sub how do I call this sub to execute from my button?
Do the textboxes have to be on the same page as the datagrid? If they don't there is a much easier way to do this, but for now here's a little explanation for you: As it stands you will have to get the RequestID from the highlighted row and use it do populate the textboxes. It looks as though you have already gotten this ID. If I was designing this page, I put a separate button to load the data into the textboxes. This button can call a LoadData function to fill the textboxes: Here is an example, sorry it is in C#: public void LoadData(string id) { // Connection string for a Text File string ConnectionString = "yourconnection"; // Open the connection SqlConnection conn = new SqlConnection(ConnectionString); conn.Open(); // Create the SQL Command string CommandString = "SELECT * FROM YOURTABLE WHERE RequestID = " + RequestID; SqlCommand comm = conn.CreateCommand(); comm.CommandText = CommandString; // Create DataReader SqlDataReader reader = comm.ExecuteReader(); // Read Data while (reader.Read()) { TextBox1.Text = reader["fieldname"].ToString(); } reader.Close(); conn.Close(); } You can use the data reader to fill the textboxes. I don't believe you will be able to use a DataAdapter as you have probably done for the DataGrid. Everything is pretty straight forward in the code above, but let me know if you need any other help. Scott Stocker