Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem reading a data value from my DataGrid

Problem reading a data value from my DataGrid

Scheduled Pinned Locked Moved ASP.NET
helpquestioncsharphtmlcss
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BlazerScott
    wrote on last edited by
    #1

    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?

    M S 2 Replies Last reply
    0
    • B BlazerScott

      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?

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi there. When some controls (like a Button) in a DataGrid triggers an event, the DataGrid fires its ItemCommand event. The DataGridCommandEventArgs will include the CommandName 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 the ItemCommand 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 on ItemCommand. Visual Studio will create the sub stub for you, then you can use an If or Select Case statement to test for "Select" as the CommandName, and execute your GetRowReqID procedure as necessary.

      1 Reply Last reply
      0
      • B BlazerScott

        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?

        S Offline
        S Offline
        sstocker
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups