Dynamic template field in grdivew post back values - asp.net

//Bedlow is the page_load code to add dynamic controls in page load. and load of page_load
gvSecond 's dynamic template fields will be added.I want to know why the dot is appended on each postback values
protected void Page_Load(object sender, EventArgs e)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
//gvFirst has gvSecond and on rowdatabound of the gvFirst gvSecond populated with dynamic template fields. and all the template fields has the TextBoxtes in it
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//below code to generate dynamic template column
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
//Added dynamic controls in gvSecond
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvFirst.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvSecond = e.Row.FindControl("gvSecond") as GridView;
DataTable dt= GetData1(Convert.ToInt32(customerId));
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();
//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
//Add the newly created bound field to the GridView.
gvSecond.Columns.Add(bfield);
}
TotalColumns = dt.Columns.Count;
gvSecond.DataSource = dt;
gvSecond.DataBind();
}
}
}
//This is used to add the dynamic template in gvSecond.Now when I click on button textbox values appended with postback values and older values seperated by dot(.)

You need to wrap the code in Page_Load in if(!IsPostback){}:
EDIT :
Edited to create second grid at postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
else
{
CreateSecondGridView();
}
}
And your Rowdatabound may look like this:
protected void gvFirst_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
CreateSecondGridView();
}
protected void CreateSecondGridView()
{
foreach(GridViewRow row in gvFirst.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string customerId = gvFirst.DataKeys[row.RowIndex].Value.ToString();
GridView gvSecond = row.FindControl("gvSecond") as GridView;
DataTable dt= GetData1(Convert.ToInt32(customerId));
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();
//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
//Add the newly created bound field to the GridView.
gvSecond.Columns.Add(bfield);
}
TotalColumns = dt.Columns.Count;
gvSecond.DataSource = dt;
gvSecond.DataBind();
}
}
}

Related

linkButton Click Event in Grid View not firing when adding though Bound Fields in a loop in GridView

Hi,Here i am trying to implement the linkButton click event in a
gridview through code behind using the BoundField Class in gridview.
when i trying to add the individual BoundField values Directly to grid
as a column in page_Load and binding the linkbutton in
RowDataBoundEvent of the row of cell with linkbutton click event, it
is firing the linkButton Click event well with the following code.
protected void Page_Load(object sender,EventArgs e)
{
BoundField bfield = new BoundField();
bfield.HeaderText = "EmpName";
bfield.DataField = "EmpName";
gridView.Columns.Add(bfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "Monday";
bfield1.DataField = "Monday";
gridView.Columns.Add(bfield1);
}
and in on RowDataBound Event i have wrote
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkViews = new LinkButton();
lnkViews.ID = "lnkViews";
lnkViews.Text = (e.Row.DataItem as DataRowView).Row["Monday"].ToString();
lnkViews.Click += new EventArgs(linkButton_ClickAction);
e.Row.Cells[2].Controls.Add(lnkViews);
}
}
protected void linkButton_ClickAction(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = row.Cells[0].Text; this.ModalPopupExtender1.Show();
}
But when i trying to add the above BoundFields by looping based on the
table columns count like this, in page_Load event, the event is not
firing.
protected void Page_Load(object sender,EventArgs e)
{
DataTable dtTable = new DataTable();
dtTable= BindGrid();
BoundField boundField;
for (int i = 0; i < dtTable.Columns.Count; i++)
{
string ColumnValue = dtTable.Columns[i].ColumnName;
boundField = new BoundField();
boundField.HeaderText = ColumnValue;
boundField.DataField = ColumnValue;
gridView.Columns.Add(boundField);
}
}
when we creating the BoudnField event using the above code in a loop
based on dataSource columns count, it doesn't firing linkbutton event. why?
as per my understanding you are trying to form grid dynamically.
One thing is bound fields will not support for bubbled events. So better replace them with template fields that will meets your requirement.
You need to assign click event in InstantiateIn method by defining ITemplate interface.
OnRowDataBound event doesn't comes into picture.
below is example for your reference.
http://forums.asp.net/t/1001702.aspx

retrieve data key of selected row in gridview in asp.net

I have a button (item template) in any rows of a gridView.
I want when user clicks on this button, I retrieve dataKey of row that there is that button in it.
how can I do this?
(sorry,I can't speak and write English very well)
Assuming you have one DataKey and it is an int and the row of the button isn't necessarily selected and you have connected the following function to the buttons in your template field:
protected void RowClicked(object sender, EventArgs e)
{
Button TempButton = sender as Button;
if (TempButton != null)
{
GridViewRow GVR = TempButton.Parent.Parent as GridViewRow;
if (GVR != null)
{
int ID = (int) GridView1.DataKeys[GVR.DataItemIndex].Value;
}
}
}
Try the SelectedIndexChanged event. Something like this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
.
.
.
}

Prepopulate gridview empty datasource

I use an gridview empty datasource for bulk insert, how would I prepopulate for instance Column A with the value of a drop down box? So far I have below, but its not working
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)e.Row.FindControl("YourTextBoxID");
if(tb != null)
{
tb.Text = DropDownList1.SelectedItem.Text;
}
}
}
I don't understand what you mean with empty DataSource. I assume that you actully mean a DataSource which is populated automatically with a default value.
You could use a DataTable:
var tbl = new DataTable();
tbl.Columns.Add("ColumnA");
var defText = DropDownList1.SelectedItem.Text;
for(int i = 0; i < 1000; i++)
{
tbl.Rows.Add(defText);
}
GridView1.DataSource = tbl;
GridView1.DataBind();

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