parent grid rowindex from child grid button click ASP.net - asp.net

I gave a nested grid. while clicking on a button in child grid I have to get parent grid row index. I have used like this type of code
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
GridViewRow Gv2Row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
GridView Childgrid = (GridView)(Gv2Row.Parent.Parent);
GridViewRow Gv1Row = (GridViewRow)(Childgrid.NamingContainer);
GridView Parentgrid = (GridView)(Gv1Row.Parent.Parent);
}
}
but it's not working..
Showing typecasting error . How to solve that..

Please try with the below code snippet.
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
GridViewRow Gv2Row = (GridViewRow)((LinkButton)sender).NamingContainer;
GridView Childgrid = (GridView)(Gv2Row.Parent.Parent);
GridViewRow Gv1Row = (GridViewRow)(Childgrid.NamingContainer);
int b = Gv1Row.RowIndex;
// Access your parent grid index here
}
}

You are missing an extra ( and ) for the code in commented line below resulting in typecasting error. And you don't need the last statement in your code because you have already found the parent grid.
I would suggest you better change the naming convention for the parent/child gridview/row object in your code to make it understand better as detailed below.
if (e.CommandName == "go")
{
// GridViewRow Gv2Row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
GridViewRow childrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
GridView parentgrid = (GridView)(childrow.Parent.Parent);
GridViewRow parentrow = (GridViewRow)(parentgrid.NamingContainer);
int parentrowindex = parentrow.RowIndex;
}

Related

How to retrieve a GridCheckBox column value through webform codebehind

I am using a telerik RadGrid that is being populated via a ObjectDataSouce. This object returns a series of boolean fields
<telerik:GridCheckBoxColumn DataField="IsSysAdmin" DataType="System.Boolean"FilterControlAltText="Filter IsSysAdmin column" HeaderText="Sys Admin"
SortExpression="IsSysAdmin" UniqueName="IsSysAdmin">
Once I select a column I would like to be able to derive the boolean value for use in another section of the page.
I can get at the values in the selected rows by doing the following:
protected void gv_roleList_Command(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
GridDataItem item = (GridDataItem)e.Item;
item.Selected = true;
txt_RoleName.Text = item["RoleName"].Text;
...edited for brevity
By calling the column I can get the values however this does not work for a GridCheckBoxColumn the text attribute only returns (which I would expect).
I have tired to cast the sender as checkbox to go at it that way but my implementation does not seem to work.
var cb = (GridCheckBoxColumn)sender;
Does anyone have any tips on how to go about deriving the value from the Checkbox column?
Cheers
Please check below code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "EditItem")
{
// if current row is in normal mode
GridDataItem item = e.Item as GridDataItem;
CheckBox chk = item["IsSysAdmin"].Controls[0] as CheckBox;
// If your row is in edit mode
GridEditableItem eitem = e.Item as GridEditableItem;
CheckBox echk = eitem["IsSysAdmin"].Controls[0] as CheckBox;
}
}

How to get the value of selected row using radiobutton in gridview in a button click

i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}

Finding controls in gridview? In Asp.net?

In web applicatiom i am trying to find grid controls in RowDataBound event. But it is giving object reference to instance of an object, this is my code :
protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
string empid = "";
empid = ((Label)e.Row .FindControl("lblname")).Text;
}
Can you hlep me please to find the control, thank you.
Ya, i got the answer, i have to place
string empid = "";
if (e.Row.RowType == DataControlRowType.DataRow)
{
empid = ((Label)e.Row.FindControl("lblname")).Text;
}
then i we get the control
Find control for Data rows only Like:
protected void mygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string empid = "";
empid = ((Label)e.Row .FindControl("lblname")).Text;
}
}
The "Object reference to instance of an object" error is probably because no control named lblname was found for the current row.
Maybe you need to check the type of the row, e.Row.RowType = DataControlRowType.DataRow so that you are not searching for the control in the header row.
Label lbl = (Label)e.Row.Cells[2].FindControl("lblCreatedBy");
lbl.Text = "ABC";

asp.net making a gridView column invisible

This is a Master-Detail form. Master is a GridView. And, the Detail is a DetailsView.
The entire thing is achieved programmatically.
As you can see from the code, DetailsView is using the Master-objects's ID to retrieve the Detail items.
I need to make the ID column of the Master-GridView invisible. Coz, it is irrelevent for the user of the page. But it must not harm the page logic.
But the code-line, GridView1.Columns[1].Visible = false; is generating an exception.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How should I solve this problem?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
List<Order> orders = Order.Get();
GridView1.DataSource = orders;
GridView1.DataBind();
// This is giving Error...............!!!
GridView1.Columns[1].Visible = false;
// At first, when the page first loads,
// GridView1.SelectedIndex == -1
// So, this is done to automatically select the 1st item.
if (GridView1.SelectedIndex < 0)
{
GridView1.SelectedIndex = 0;
}
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView1.Rows[selRowIndex].Cells[1].Text);
Order master = Order.Get(selMasterId);
labItemsCount.Text = master.Items.Count.ToString();
DetailsView1.DataSource = master.Items;
DetailsView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
BindData();
}
}
Have you considered using the DataKeyNames property of the gridview? This way you can remove the 'id' column from the GridView bu still access the 'id' value in the Page_Load.
DataKeyNames = "id"
Then you can get the value of the id like this.
int selRowIndex = GridView1.SelectedIndex;
int selMasterId = Convert.ToInt32(GridView.DataKeys[selRowIndex].Value);
Order master = Order.Get(selMasterId);
Alternately, you could try changing the visibility of the column in the OnRowBound event of the GridView.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header ||
e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Visible = false;
}
}

How to add a row in asp.net grid view

So far I have done this, I am not sure whether this is right or wrong
public partial class _Default : System.Web.UI.Page
{
Label l = new Label();
GridView gv = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
GridViewRow gvr = new GridViewRow(i, i, DataControlRowType.DataRow, DataControlRowState.Normal);
gvr.Controls.Add(l);
gv. (what to do here)
}
this.Controls.Add(gv);
}
}
please help
gv.Rows.Add(gvr);
If you're starting with an empty GridView, an easier way to dynamically create x rows is to create a dummy list and then set it to the data source:
var list = new List<string>(10); // replace 10 with number of empty rows you want
// for loop to add X items to the list
gv.DataSource = list;
gv.DataBind();
If you are doing this, I'd recommend doing it with a Repeater. It's a lot easier to manage.
The DataGrid fires the RowCreate event when a new row is created.
Collapse
//OnRowCreated="GridView3_RowCreated"
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
//When a child checkbox is unchecked then the header checkbox will also be unchecked.
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("chkselect");
CheckBox chkBxHeader = (CheckBox)this.GridView3.HeaderRow.FindControl("chkHeader");
chkBxSelect.Attributes["onclick"] = string.Format("javascript:ChildClick(
this,'{0}');", chkBxHeader.ClientID);
}
}

Resources