Badvoc
Badvoc

Reputation: 11

select data from gridview field and populate textbox

I'm trying to populate a single text box (or parameter) with data from a gridview column when I click on a button in that row. Gridview gets it data from a sqlconnection

the gridview is

| Drawing |

| 12345 | VIEW

| 12346 | VIEW

the VIEW is a template button with an onclick event, when the user clicks the button the data from the Drawing column (12345) should be passed to ether a textbox or a paremeter. (this is the part I dont know how to do) once the Iv got the number in a textbox I can use it as pareameter and then a pdf is opened of that drawing, I have code for this and is working.

thanks for any help

Upvotes: 0

Views: 8503

Answers (2)

Nick
Nick

Reputation: 56

If you are using C#, the simplest thing to do would be to add an in-built select command button to the gridview rows at runtime. Then on the selectedindexchanged event of the gridview simply access the cell of the selected row that you want the value from. You can then assign that string to anything you want. Like so:

protected void myGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        string myString = myGridView.SelectedRow.Cells[4].Text.ToString();
        TextBox1.Text = myString;
    }

Remember that the cell index collection is zero based, so [0] is actually the first cell in the row.

Upvotes: 2

Karl Anderson
Karl Anderson

Reputation: 34846

Use TemplateFields and the grid view's OnRowCommand event, like this:

Markup:

<asp:gridview id="GridView1" 
              OnRowCommand="GridView1_RowCommand"
              runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBoxDrawing" runat="server" 
                             Text="<%# Eval("Drawing")) %>" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="selc" runat="server" Text="View" 
                    CommandName="View" 
                    CommandArgument="<%# ((GridViewRow)Container).RowIndex %> />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked
    if(e.CommandName == "View")
    {
        // Convert the row index stored in the CommandArgument
        // property to an integer
        var index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection      
        var row = GridView1.Rows[index];

        // Find the drawing value
        var theDrawingTextBox = row.FindControl("TextBoxDrawing") as TextBox;

        // Verify the text box exists before we try to use it
        if(theDrawingTextBox != null)
        {
            var theDrawingValue = theDrawingTextBox.Text;

            // Do something here with drawing value
        }
    }
}

Upvotes: 0

Related Questions