How can I show my Gridview first row as Header text? - asp.net

I have a gridView I want to set its first 11 columns as Heading like="NMAT APPLICANT DETAILS". My gridview name is GridView1. How can I set this?
I done it by below mentioned method. Now how can I adjust this heading center of this 11 cells??
My code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "NMAT APPLICANT DETAILS";
HeaderCell.ColumnSpan = 11;
HeaderGridRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}

nothing was there I hust done like below mentioned methode
HeaderCell.HorizontalAlign = HorizontalAlign.Center;

Related

how to add footer template dynamically in asp.net

I have created a gridview dynamically. There is template fields described in designing portion. All the columns were created thru code behind as follows. Its works fine. Here I can listed the pages for each rows. But I dont know how to implement the sum of pages in the footer template thru code behind.
TemplateField Pages = new TemplateField();
Pages.HeaderText = "Pages";
Pages.ItemTemplate = new GridViewTemplate_Pages();
gv1.Columns.Add(Pages);
public class GridViewTemplate_Pages : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
Label PagesLabel = new Label();
PagesLabel.DataBinding += new EventHandler(this.PagesLabel_DataBinding);
container.Controls.Add(PagesLabel);
}
void PagesLabel_DataBinding(object sender, EventArgs e)
{
Label lbl1 = (Label)sender;
GridViewRow row = (GridViewRow)lbl1.NamingContainer;
lbl1.Text = DataBinder.Eval(row.DataItem, "PagesReceived").ToString();
}
}
Given ShowFooter="True" in aspx page and RowDataBound written separately. The following code works fine if I given footer template in aspx page but do not know how to get the result in programmatically. Please advice.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
TotalPages = TotalPages + RowTotalPages;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label m = (Label)e.Row.FindControl("gv1TotalPages");
m.Text = TotalPages.ToString();
}
}
You can create the footer to a gridview like this.
//Code
GridView gv = new GridView();
gv.RowCreated += delegate(object dsender, GridViewRowEventArgs ge)
{
if (ge.Row.RowType == DataControlRowType.Footer)
ge.Row.Cells[0].Text = "Something";
};
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
BoundField bf = new BoundField();
bf.HeaderText = "col 1";
bf.DataField = "Length";
gv.Columns.Add(bf);
gv.DataSource = new string[] { "One", "Two", "Three" };
gv.DataBind();
Form.Controls.Add(gv);
This is for the dynamically created gridview and a footer. You can change accordingly.
Its a silly thing. I made it complex. I need the sum of pages in the footer row, then why should I think and confuse about GridViewTemplate and Itemplate class. Simply I added the Total pages in the session and recollect it in the DataControlRowType.Footer. Here is the code:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
int RowTotalCharges = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ChargesReceived"));
TotalPages = TotalPages + RowTotalPages;
TotalCharges = TotalCharges + RowTotalCharges;
Session.Add("TotalPages", TotalPages.ToString());
Session.Add("TotalCharges", TotalCharges.ToString());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "Batches Count: " + gv1.Rows.Count.ToString();
e.Row.Cells[5].Text = Session["TotalPages"].ToString();
e.Row.Cells[6].Text = Session["TotalCharges"].ToString();
}
}

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();

Add second header row to GridView BELOW already existing header row

I'm trying to dynamically insert a 2nd header row at a GridView's OnRowCreated event. However, I can't seem to get the row to be inserted anywhere besides the first spot in the gridview's row index. The code below fails on the last line, where the header is actually added to the grid. Index out of bounds exception. How can I add this header row below the already existing header row? Help is much appreciated, thanks!
protected void gvwProd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView gvw = (GridView)sender;
GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Prod Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 4;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Materials Comments - TS/LD";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 8;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Plant Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 11;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
gvw.Controls[0].Controls.AddAt(1, HeaderRow);
}
}
Had a similar problem. You could try casting the GridView to a Table and access the .Rows method:
((Table)gvw.Controls[0]).Rows.AddAt(1, HeaderRow);
source

ASP.NET Nested GridView, DataItem returns null in child GridView's RowDataBound Event

In a nested GridView (GridView inside a template column of parent GridView). I am binding child GridView to a DataTable in parent GridView's RowDataBound event. this works as it should. But the problem i am facing is in Child GridView's RowDataBound Event, when i try to access e.Row.DataItem property it returns null. I am expecting it to return DataRowView Type. which i will then use to set values of TextBox's.
Parent GridViewId = gvProductOptionGrps and
Child GridViewId = gvProductOptions
Parent GridViews RowDataBound Event.
protected void gvProductOptionGrps_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//ProductOptionGrps
TextBox txtProductOptionGrpSortOrder = (TextBox)e.Row.FindControl("txtProductOptionGrpSortOrder");
Label lblProductOptionGrpName = (Label)e.Row.FindControl("lblProductOptionGrpName");
DataRowView drv = (DataRowView)e.Row.DataItem;
txtProductOptionGrpSortOrder.Text = drv["SortOrder"].ToString();
lblProductOptionGrpName.Text = drv["Name"].ToString();
//ProductOptions
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "SELECT a.ProductOptionId,a.SortOrder,b.Name,b.PriceGBP" +
" FROM ProductOptionGrpProductOptions a" +
" INNER JOIN ProductOptions b ON a.ProductOptionId=b.ProductOptionId" +
" WHERE a.ProductOptionGrpId=#ProductOptionGrpId" +
" ORDER BY a.SortOrder";
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Parameters.Add("#ProductOptionGrpId", SqlDbType.UniqueIdentifier).Value = new Guid(drv["ProductOptionGrpId"].ToString());
_fl.ConnectToSQLServer();
sqlCmd.Connection = _fl.GetActiveSQLServerConnection();
DataTable dtProductOptions = new DataTable();
dtProductOptions.Load(sqlCmd.ExecuteReader());
GridView gv = (GridView)e.Row.FindControl("gvProductOptions");
gv.DataSource = dtProductOptions;
gv.DataBind();
_fl.DisconnectFromSQLServer();
}
}
protected void gvProductOptions_RowDataBound(object sender, GridViewRowEventArgs e)
{
TextBox txtProductOptionSortOrder = (TextBox)e.Row.FindControl("txtProductOptionSortOrder");
TextBox txtPriceGBP = (TextBox)e.Row.FindControl("txtPriceGBP");
DataRowView drv = (DataRowView)e.Row.DataItem;//returns null
txtProductOptionSortOrder.Text = drv["SortOrder"].ToString();//Error
txtPriceGBP.Text = drv["PriceGBP"].ToString();//Error
}
You have forgotten to check if the Row is a DataRow in gvProductOptions_RowDataBound. The Header does not have a DataItem hence it is null.
if (e.Row.RowType == DataControlRowType.DataRow)

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