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..

Recommended Answers

All 6 Replies

What do you have so far? post some code so we can help you out to fix it.

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.

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

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!!!.

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

problem is solved...Thanks for the help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.