I have a gridview in user control and added the controls dynamically using placeholder
now i find the controls from grid it give that controls are not exist in the grid
Code is as Following :
foreach (GridViewRow row in GridView1.Rows)
{
PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder");
TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its give Null
}
can anyone has answer plz
Added code from Comments:
foreach (GridViewRow dr in GridView1.Rows)
{ PlaceHolder placeHolder = dr.FindControl("lblPlaceHolder") as PlaceHolder;
TextBox txtTextBox1= new TextBox();
txtTextBox1.Width = 300;
placeHolder.Controls.Add(txtTextBox1);
}
Remove the placeholder, and You will be able to find your controls
and also find your controls using the onRowDatabound, or onRowCreated event from the grid.
btw: why is your placeholder called lblPlaceHolder, do You have a label in there and maybe you are asking the wrong contol to find yr textbox, thats why you are getting a null
void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row as GridViewRow;
if (row != null)
{
TableCell myCell = row.Cells[0] as TableCell
TextBox txtTextBox1= new TextBox();
txtTextBox1.Width = 300;
myCell.Controls.Add(txtTextBox1);
}
}
Related
I have a gridview and on some condition basis, I am inserting text box in it in RowDataBound:
private void GetColumnWithValidation(GridViewRowEventArgs e, string columnName, int columnLength)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName)).Length > columnLength)
{
int colindex = GetColumnIndexByName(e.Row, columnName);
TextBox txt = new TextBox();
txt.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName));
txt.BorderColor = Color.Red;
txt.ID = "txt_" + i + "_" + colindex;
lstErrorTracker.Add(i + "_" + colindex);
//link.NavigateUrl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "PV_INVOICE_LOCATION"));
e.Row.Cells[GetColumnIndexByName(e.Row, columnName)].Controls.Add(txt);
}
}
Now if I change some data in text box of gridview I want that to be fetch whenever I click Update button. Update button is placed outside of gridview as a normal asp.net button.
But when I am trying to fetch the data I am getting null.
foreach (GridViewRow row in GVUploadDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox txt= (row.Cells[3].FindControl("txt_0_3") as TextBox);
var test = txt.Text;
}
}
I am getting text data as null. Please help how to get those values. And on other hand I want the entire data of the gridview as a Dataset or datatable how can I get that.
The NamingContainer is the GridViewRow not the cell, so you should use:
foreach (GridViewRow row in GVUploadDetails.Rows)
{
TextBox txt = row.FindControl("txt_0_3") as TextBox;
// ...
}
If this doesn't fix the issue, where in the page's life-cycle do you addd this TextBox programatically? You know that you always have to re-create it on every consecutive postback? Do that latest in Page_Load, better Page_Init.
If you add it from RowDataBound you have to re-create it in RowCreated, otherwise it's too late to retain the ViewState.
I have a gridview,in which there is item template..in this template there is a control.I need to find this control using find control method.
How can i achieve this
Try this:
foreach(GridViewRow row in GridViewName.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
Control myControl = row.FindControl("ControlID") as Control;
}
}
CheckBox chkDelete = (CheckBox)grdInbox.Rows[0].Controls[5].FindControl("chkDelete");
I have the following code :
Label docsLabel = new Label();
docsLabel = (Label)tasksPlaceholder.FindControl("taskdocs_" + taskId);
int index = tasksPlaceholder.Controls.IndexOf(docsLabel);
The label is found within the placeholder, but when I call .IndexOf() it always returns -1.
How do I find the correct position of this control?
This is an important information in your comments:
the element I want to update is 3 levels down (TableRow -> TableCell ->Label)
Control.FindControl finds all control in this NamingContainer whereas ControlCollection.IndexOf finds only controls in this control. So if this control contains for example a table which contains rows and cells and every cell contains also controls, all of these controls will not be found by IndexOf, only the top-control is searched.
Control.FindControl will search all controls that belong to this NamingContainer(a control that implements INamingContainer). A table/row/cell does not implement it, that's why all of these controls are also searched with FindControl.
However, FindControl will not search through sub-NamingContainers (like a GridView in a GridViewRow).
This reproduces your issue:
protected void Page_Init(object sender, EventArgs e)
{
// TableRow -> TableCell ->Label
var table = new Table();
var row = new TableRow();
var cell = new TableCell();
var label = new Label();
label.ID = "taskdocs_1";
cell.Controls.Add(label);
row.Cells.Add(cell);
table.Rows.Add(row);
tasksPlaceholder.Controls.Add(table);
}
protected void Page_Load(object sender, EventArgs e)
{
Label docsLabel = (Label)tasksPlaceholder.FindControl("taskdocs_1");
int index = tasksPlaceholder.Controls.IndexOf(docsLabel);
// docsLabel != null and index = -1 --> quod erat demonstrandum
}
How do I find the correct position of this control?
If you want to find the row-number this label belongs to:
Label docsLabel = (Label)tasksPlaceholder.FindControl("taskdocs_1");
TableRow row = (TableRow)docsLabel.Parent;
Table table = (Table)row.Parent;
int rowNumber = table.Rows.GetRowIndex(row);
I have a DataGridView with Different Control Type in Each row now i want to create control in each row according to that Control Type Is it Possible?
if Yes then How ?
You can add control on the OnRowDatabound of your grid, then you can a controls to your row
To add a control to a cell use
void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row as GridViewRow;
if (row != null)
{
MyObject myObject = new MyObject();
row.Cells[0].Controls.Add(myObject);
}
}
To add a control to the entire row use
void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row as GridViewRow;
if (row != null)
{
MyObject myObject = new MyObject();
row.Controls.Add(myObject);
}
}
Have a look here for the windows forms Grid
You can create your own column class by inheriting the DataGridViewColumn class or any of its derived classes to provide custom appearance, behavior, or hosted controls. For more information, see How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance
http://msdn.microsoft.com/en-us/library/bxt3k60s%28v=vs.80%29.aspx
How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance
http://msdn.microsoft.com/en-us/library/7fb61s43%28v=vs.80%29.aspx
I have a gridview with 10 rows i am displaying 6 rows in each page i have a text box and and image button in each row when i click the image button all the functionalities are working but when i click the page index it is displaying an error in row command how can i check whether the row type is data row or not in gridview row command event. the code that i am using is as follows
protected void gvgridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
TextBox txtgvGroupName = (TextBox)gvRow.FindControl("txtgvGroupName");
ImageButton imgbtn = (ImageButton)gvRow.FindControl("imgbtn");
if (e.CommandName == "Edit")
{
imgbtn.Visible = false;
}
}
If(e.Row.RowType == DataControlRowType.DataRow)
then write your condition.
a little to do with the datarow here, instead you need to check if (e.CommandSource is ImageButton) at the first line of your gvgridview1_RowCommand
Have you tried checking the RowType property of the GridViewRow instance?
try this assuming you are reffering System.Data.DataRow
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
DataRow drow = gvRow.DataItem as DataRow
if(drow!=null)
{
// row is DataRow
}
Check e.Row.RowType, it allows you to compare the RowType to the DataControlRowType enum.
[EDIT] I wonder if it's something to do with how you're getting hold of the row. The code I just tried is:
GridViewRow row = Gridview.Rows[int.Parse(e.CommandArgument.ToString())];
After that I can use row.RowType quite happliy. Might be worth a try for you.
DataControlRowType