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;
Related
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.
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
I have a datagridview which is loaded through a strored procedure. It has an ID column which i want to hide it but still access its value. I set the dataKeyNames and i have a method
protected void grdTime_OnDataBound(object sender,EventArgs e)
{
grdTime.Columns[1].Visible = false;
}
which i am trying to hide this column but i get the following error. If i remove the line inside the method it works. Here is the error.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Try this code in the method and then check..
grdTime.Columns[0].Visible = false;
hide the column in Row_Created event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
}
here hiddencol is a css class
.hiddencol
{
display:none;
}
in the above code i'm hiding second column of my grid
If you've set GridView's AutoGenerateColumns to true(default), the Columns-Collection is empty. Following should work then:
1.)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
2.)
foreach (TableRow row in GridView1.Controls[0].Controls)
{
row.Cells[0].Visible = false;
}
you can use like this in gridview tag on .aspx page
<Columns>
<asp:BoundField HeaderText="Merchandise Id" DataField="MerchandiseId" Visible="false" /></columns>
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.
I have nested a repeater control in Gridview. Right now it is showing gridview rows and repeater header for every case(whether data is there or not for that particular grid view row in the repeater control). I want to hide the gridview row and repeater control header when there is no data present for that particular gridview row.
Thanks,
That case I handled at code level by filtering the resulted data table.
Now the another problem I am facing:
I have allowed the paging on the gridview i.e. pagesize 3.
When page loads it works fine, but when I go to page 2 then it generates following error:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Below is the code to fill the grid, paging and fill repeater on rowdatabound event of grid.
private void FillGrid()
{
clsCustomFunctions objShort = new clsCustomFunctions();
grd1.DataSource = objShort.GetAll();
}
protected void grd1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
FillGrid();
grd1.PageIndex = e.NewPageIndex;
grd1.DataBind();
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
}
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
clsCustomFunctions objShort = new clsCustomFunctions();
if (e.Row.RowType == DataControlRowType.DataRow)
{
((HtmlTable)e.Row.FindControl("gridTable")).BgColor = "#006699";
Repeater rpt = (Repeater)e.Row.FindControl("rpt1");
rpt.DataSource = objShort.GetResult(Convert.ToInt32(grd1.DataKeys[e.Row.DataItemIndex].Value));
rpt.DataBind();
}
}
grd1.DataKeys[e.Row.DataItemIndex].Value line is throwing error. How to handle this to pass values of page 2 only.
Try handling the OnRowDataBound event of the grid. This gives you a GridViewRowEventArgs object (say e).
You can then look at e.Row.DataItem to get the data it is binding to to check if you need to hide the header.
You can use e.Row.FindControl("RepeaterName") to get the repeater to manipulate as you want.