2

I have one gridview where I am passing the command argument as gridview row id for the Button I created for every row.

I want to display all the details of that row in the textbox according to the Row clicked.

<asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="false" 
              onrowcommand="gvCategory_RowCommand" >
          <Columns>
            <asp:TemplateField>
              <ItemTemplate>
                 <asp:Label ID="lblCatId" runat="server" Text='<%#Eval("categoryId") %>'></asp:Label>
              </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField>
              <ItemTemplate>
                 <asp:Label ID="lblCatName" runat="server" Text='<%#Eval("categoryName") %>'></asp:Label>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
              <ItemTemplate>
                <asp:Button ID="btnModify" runat="server" Text="Modify" CommandName="Modify" CommandArgument='<%#Eval("categoryId") %>' />
              </ItemTemplate>
            </asp:TemplateField>
          </Columns>
          </asp:GridView>

Code.....

if (e.CommandName == "Modify")
{
     int id = Convert.ToInt32(e.CommandArgument);          
     // I want the value to assgin for the selected row here
     // I was previously fetching the data from database according id,but i want this frim the gridview of selected row.
}
3
  • Have you verified that e.CommandArgument is the expected categoryID? Commented Nov 29, 2010 at 17:07
  • I would suggest changing your question title to reflect the actual problem too. Commented Nov 29, 2010 at 17:47
  • Are you still struggling with this? If my answer helped you, please mark it so that others users can benefit from it as well. Commented Nov 30, 2010 at 18:59

2 Answers 2

2

I wrote a quick example of how to do what you're trying to do. It works for me.

The Example Solution

Default.aspx

<asp:GridView ID="myGridView" runat="server"
    AutoGenerateColumns="False"
    DataSourceID="StudentsDS"
    DataKeyNames="ID"
    OnRowCommand="myGridView_RowCommand"
    OnSelectedIndexChanged="myGridView_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
            SortExpression="ID" />
        <asp:BoundField DataField="FullName" HeaderText="FullName" 
            SortExpression="FullName" />
        <asp:BoundField DataField="ClassID" HeaderText="ClassID" 
            SortExpression="ClassID" />
        <asp:CommandField ShowSelectButton="True" SelectText="Modify" />
    </Columns>
</asp:GridView>
<asp:TextBox ID="txtStudent" runat="server" />
<asp:SqlDataSource ID="StudentsDS" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Sandbox %>" 
    SelectCommand="SELECT * FROM Student"
/>

Default.aspx.cs

protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e) {
    if (e.CommandName == "Select") {
        // do something here if you want, although not necessary
    }
}

protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
    // show "FullName" field of selected row in textbox
    txtStudent.Text = myGridView.SelectedRow.Cells[1].Text;
}

How It Works

Upon clicking "Modify" in a row, the textbox updates to show the FullName field of the selected row.

The important part is that instead of a TemplateField, use a CommandField with ShowSelectButton="True". Then do what you need to do in the SelectedIndexChanged event handler. Note that the SelectText of the CommandField is set to "Modify" as you desired. You can also set the ButtonType property of the CommandField to be button, image, or link.

Making It Better

I would also advise that instead of using a SqlDataSource as I have, you use an ObjectDataSource so that you can do something like

protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
    MyModelObject o = myGridView.SelectedRow.DataItem as MyModelObject;
    txtStudent.Text = o.MyProperty;
}

You may also consider wrapping your GridView in an UpdatePanel to prevent full postbacks / page refreshes.

Sign up to request clarification or add additional context in comments.

Comments

0

After you have the lineID, you select that line, and then you have the CategoryID on selected value

int iTheIndexNow = Convert.ToInt32(e.CommandArgument);    

// select that line to see it visual as selected, and get the value on next line
gvCategory.SelectedIndex = iTheIndexNow;    

// here is your categoryID that   
//you can use to open your database with and get the text
gvCategory.SelectedValue // == categoryID

On GridView you must have set your KeyID

<asp:GridView DataKeyNames="categoryId" .... >

5 Comments

I want the value to display in the text box which i selected in the grid view.
@user373083 I do not see the textbox, where is it ? , anyway just type txtBox.Text = gvCategory.SelectedValue;
protected void gvCategory_RowCommand1(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Modify") { int id = Convert.ToInt32(e.CommandArgument); txtCategory.Text = gvCategory.Rows[id].Cells[2].Text.ToString(); } }
@bitxwise Yes its read only, is the CategoryID that can use to read the database selected line and use it.
My comment was incomplete, but it doesn't matter anymore =) Sorry for the confusion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.