How to populate textbox with value of gridview control? - c#

With just a regular value you would write something like
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = email.Cells[5].Text;
}
However, I want to populate that textbox with a value that is a textbox control in the gridview. I have a list of rows and depending on which row I select that textbox will populate with that control value. Any help/advice would be greatly appreciated.

I would suggest using datakeys for this. It's a lot easier and a lot more reliable than using the cell index:
<asp:GridView ID="Gridview1" runat="server" DataKeyNames="Column1, Column2" ...>
Then, in the code-behind you can access the values like this:
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow email = GridView1.SelectedRow;
txtbox.Text = (string)GridView1.DataKeys[email.RowIndex]["Column1"];
}

Try this:
GridView1.RowCommand += GridView1_RowCommand;
private void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(Convert.ToString(e.CommandArgument));
GridViewRow row = GridView1.Rows[index];
this.NameTextBox.Text = Server.HtmlDecode(row.Cells[1].Text);
}
}

Try below code.
TextBox tb = GridView1.SelectedRow.FindControl("textboxId") as TextBox;
textbox.Text = tb.Text;

You can find a better code here on how to populate gridview with the text box values of your form.
http://sharepoint-2010-world.blogspot.in/2013/10/populating-grid-with-form-values.html

Related

How to define grid view inside RowCommand

I have the following gridviews gvAgreement and nested grid-view gvProducts
I am looking for defining gvProducts inside gvAgreement_RowCommand
I have defined gvProducts in gvAgreement_OnRowDataBound
by the following code
protected void gvAgreement_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gvProducts = e.Row.FindControl("gvProducts") as GridView;
}
protected void gvAgreement_RowCommand(object sender, GridViewCommandEventArgs e)
{
//your suggest code
}
This will let you to find any child control inside parent gridview.
GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
Int32 rowIndex = gvRow.RowIndex; // required if you want to find index of the control from where event has been raised.
GridView gvProducts = gvRow.FindControl("gvProducts") as GridView;

How can i get gridview row count

I am trying to get gvProducts row count.
I have tried the below code but it give insufficient result.
protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
if (row.Controls.Count == 1)
{
//my code
}
}
You want total rows in Gridview? Use Count property:
gvProducts.Rows.Count
Update:
To find the rows count of nested gridview, you can use the RowDataBound event of parent gridview:-
protected void gvProductsParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvProducts = (GridView)e.Row.FindControl("gvProducts ");
int count = gvProducts.Rows.Count;
}
}
Please note this event will fire for each row present in your parent gridview this the count will change according to each row.

how to change drop down list based on gridview selection

I have a drop down list which will load up with a series of customer ID numbers. I then have a gridview control on my page with the select hyperlink. When i click on the link to select the row in gridview i would like my dropdown to change to that number.
Below is what i have tried but doesn't work:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (!GridView1.SelectedIndex.Equals(-1))
{
DropDownList ddl;
ddl = (DropDownList)form1.FindControl("ddl_Customers");
ddl.SelectedValue = (String)GridView1.SelectedDataKey.Values[0];
}
}
Handle the SelectedIndexChanged event for GridView1
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedDataKey.Value.ToString();
}
You can directly use GridView1.SelectedValue.ToString() for this. To use that, you should define a datakeyname like this: <asp:Gridview DataKeyNames="CustomerID">
Then all you need is this:
void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
ddl_Customers.SelectedValue = GridView1.SelectedValue.ToString();
}

Select and show selected row to label

So, when I click Select I want to show in a Label all datas that contain one row. I have managed to make this, except DropdownList. When I click "Select" it's just empty.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Label2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Label3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
P.S.: I have not done this programmatically. The only code I've wrote on .aspx.cs file is the code above.
Use this to find the value. its not showing the value because of template field control.
I have used the gridview control that you pasted in pastebin.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
Label drpValue =
(Label)this.GridView1.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1");
Lbl1.Text = drpValue.Text;
Lbl2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Lbl3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}

Accessing Gridview columns by Row.Cells

Hai I am using the following code to access columns in a row but always displays empty string when using Row.Cells[1].Text I am using this code in GridView Unload event handler.
Thanks inn advance.
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
I would suggest using the Gridview Databound event instead. Because the
Unload event occurs when the server control is unloaded from memory.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
}
Databound events occur after the server control binds to a data source.
To understand how gridview events work, look at MSDN
It could be done in Gridviews RowDataBound Event ie
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
String coltext = e.Row.Cells[1].Text
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
String coltext = e.Row.Cells[1].Text
}
}
Hope this helps.

Categories

Resources