0

hi..

im using a grid with a template field which contains a button.i just want to know how can i retrive a value from a 2nd cell of the same row...
for ex. my grid contains 5 columns.the last col is a template field with a button. ther are say 10-15 records in the grid & i want to retrieve a value from the 2nd cell of the same row that the button is clicked....wht steps i need to take for the same?

Please reply soon..

Thanks..

4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by love_dude1984
0

I thought that you have use commandargument as an id for that row.
Here is a sample code so that you get an idea how one can do this:

foreach (GridViewRow oItem in gvListofSources.Rows)
            {
                clsDbUtility.ExecuteNonQuery("UPDATE Source SET txtStatus='" + ((DropDownList)oItem.FindControl("cboStatus")).SelectedValue + "' WHERE ID=" + oItem.Cells[0].Text);
            }

If you look at the code here i grab a dropdownlist from a griview row. You can also use datakeynames.

0

well...i have a code for checkbox's checkedchange event. which is as follows

for (int i = 0; i < GridView2.Rows.Count; i++)
        {            
            CheckBox chk1 = (CheckBox)(GridView2.Rows[i].Cells[0].FindControl("chk"));
            if (chk1 != null)
            {
                if (chk1.Checked)
                {
                    count++;
                }
                else
                {
                    if (count == 0)
                    {
                    
                    }                    
                }
            }
            if (count == 0)
            {
                btnRemove.Enabled = false;
               // btnSubQuery.Enabled = false;                
            }
            else
            {
                btnRemove.Enabled = true;
               // btnSubQuery.Enabled = true;                
            }
        }

im trying to modify the same code so that ill be able to retrieve a value..but im nt getting what code i should write to know if the button is pressed or nt? for checkbox ive used

if (chk1.Checked)

...but what i can use it for button???

ill explain my problem again..

i have a gridview, with the data taken from database.im using all the template fields for my columns & binding the data dynamically. now, in the last column, im using a button which, when clicked, should return a value from the 2nd template fields label of the same row...or, is ther any other way ill be able to retrieve the value?
its not also goin into the Click method when i checked it by a breakpoint. what would be the prob?

Plz reply me soon..

thanks

Edited by love_dude1984: n/a

Attachments prob.PNG 11.79 KB
0

OK this is what you have to do. lets say i have this datagrid with this linkbutton it does not matter just replace the linkbutton for regular button

Properties to take a look, CommandName and OnRowCommand

<asp:GridView ID="gvTools" runat="server" OnRowCommand="GetDetails" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkView" runat="server" Text="View" CommandName="View"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

then and the background you have a function like this.

protected void GetDetails(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "View")
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
//Just change the number for the cell you want.
                String variable = selectedRow.Cells[4].Text;
}
}

I hope that helps!!!.

Edited by jbisono: n/a

0

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Show")
{
Button b = (Button)GridView1.Rows[int.Parse
(e.CommandArgument.ToString())].Cells[1].
FindControl("Button1");
if (b.Text == "Show")
{
string custid = GridView1.DataKeys[int.Parse
(e.CommandArgument.ToString())].Value.ToString();
SqlDataSource sds = (SqlDataSource)GridView1.
Rows[int.Parse(e.CommandArgument.ToString())].
FindControl("SqlDataSource2");
GridView gv = (GridView)GridView1.Rows[int.Parse
(e.CommandArgument.ToString())].FindControl("GridView2");
sds.SelectParameters[0].DefaultValue = custid;
gv.Visible = true;
b.Text = "Hide";
}
else
{
GridView gv = (GridView)GridView1.Rows[int.Parse
(e.CommandArgument.ToString())].FindControl("GridView2");
gv.Visible = false;
b.Text = "Show";
}
}
}


may be it work

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.