How to add TemplateField programmatically

This might help to get started: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var linkField = new TemplateField(); linkField.ItemTemplate = new LinkColumn(); GridView1.Columns.Add(linkField); } } class LinkColumn : ITemplate { public void InstantiateIn(System.Web.UI.Control container) { LinkButton link = new LinkButton(); link.ID = “linkmodel”; container.Controls.Add(link); } } But: Although you can dynamically add … Read more

Columns of two related database tables in one ASP.NET GridView with EntityDataSource

OK, much too many hours later I found the solution myself: Option 1: It is possible to use the select property in the EntityDataSource which allows to create arbitrary projections of data from several related entities/database tables (in my case: OrderID from Order entity and City from the Address entity) Drawback: Using select in the … Read more

Gridview row editing – dynamic binding to a DropDownList

Quite easy… You’re doing it wrong, because by that event the control is not there: protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { // Here you will get the Control you need like: DropDownList dl = (DropDownList)e.Row.FindControl(“ddlPBXTypeNS”); } } That is, it will only be … Read more

Prevent user from resizing columns with WPF ListView

For those looking for a quicker and simpler answer. Set IsEnabled to False in the ColumnHeaderContainerStyle. This will prevent the user from resizing. Like this: <GridView.ColumnHeaderContainerStyle> <Style TargetType=”{x:Type GridViewColumnHeader}”> <Setter Property=”IsEnabled” Value=”False”/> </Style> </GridView.ColumnHeaderContainerStyle> If you want to fix the disabled grayed out color add a trigger on the IsEnabled property and fix what you … Read more

Implement JQuery Datatable in ASP.NET GridView

These few lines are all you need to get it working. You don’t need the prerender event. Just bind in Page_Load in the IsPostBack check. I did add a RowDataBound event to the GridView to add the <thead> and <tbody> sections programatically rather than with jQuery. <script type=”text/javascript” src=”https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js”></script> <link type=”text/css” rel=”stylesheet” href=”https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css” /> <asp:GridView … Read more

Change cell color on different values – Gridview

This is called Conditional Formatting You can enable the RowDataBound Event in the markup <asp:GridView ID=”gridview1″ runat=”server” OnRowDataBound=”RowDataBound”> </asp:GridView> And put this in your Code-Behind file. protected void RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { if(e.Row.RowIndex == 0) // This is row no.1 if(e.Row.Cells[0].Text == “ABC”) e.Row.Cells[0].BackColor = Color.Red; if(e.Row.RowIndex == 1) // … Read more

GridView with merged cells

You will have to use RowSpan. Refer following code for it: protected void GridView1_DataBound1(object sender, EventArgs e) { for (int rowIndex = GridView1.Rows.Count – 2; rowIndex >= 0; rowIndex–) { GridViewRow gvRow = GridView1.Rows[rowIndex]; GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1]; for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++) { if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text) { … Read more