0 голосов

Пожалуйста, войдите или зарегистрируйтесь чтобы ответить на этот вопрос.

0 голосов
ответил от
Вот решение, на которое оно может помочь.

Страница/контрольная разметка:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicGridview.aspx.cs" Inherits="CodeProject.Com.DynamicGridview" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title> </head> <body>     <form id="form1" runat="server">         <div>             <asp:GridView runat="server" ID="Gridview1" AutoGenerateColumns="false" OnDataBinding="Gridview1_DataBinding" OnRowDataBound="Gridview1_RowDataBound">             </asp:GridView>         </div>     </form> </body> </html>


Код за:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CodeProject.Com {     public partial class DynamicGridview : System.Web.UI.Page     {         private IEnumerable<DashboardPrice> dashboardPrices;         private string itemCode = "";         private int billNo = 1;         /// <summary>         /// Use this event to get the default data before hand.         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         protected void Page_PreInit(object sender, EventArgs e)         {             int mockDataCount = Convert.ToInt32(Request.QueryString["count"]);             itemCode = Request.QueryString["itemcode"] ?? "";             billNo = Convert.ToInt32(Request.QueryString["billno"]??"0");             this.dashboardPrices = MockDashboardPrice.GetMockDashboardPrice(mockDataCount);             ((List<DashboardPrice>)this.dashboardPrices).Add(new DashboardPrice()             {                 Price = 10,                 PriceName = "MRP",                 Item = "Item_10",                 Bill = 10             });         }         protected void Page_Load(object sender, EventArgs e)         {             if (!Page.IsPostBack)             {                 Gridview1.DataSource = this.dashboardPrices;                 Gridview1.DataBind();             }         }         /// <summary>         /// Before databinding completes, the fields are programatically added.         /// To add dynamically, use reflection to go through the object properties.         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         protected void Gridview1_DataBinding(object sender, EventArgs e)         {             AddGridviewColumn("Price");             AddGridviewColumn("PriceName");             AddGridviewColumn("Item");             AddGridviewColumn("Bill");             AddGridviewColumn("MRP");             AddGridviewColumn("GP");         }         protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)         {             if (e.Row.RowType == DataControlRowType.DataRow)             {                 AddDashboardPriceToGridview((DashboardPrice)e.Row.DataItem, e.Row);             }         }         private void AddGridviewColumn(string name)         {             TemplateField col = new TemplateField();             col.HeaderText = name;             Gridview1.Columns.Add(col);         }         /// <summary>         /// Sets the gridview row values of dashboard price item.         /// </summary>         /// <param name="item"></param>         /// <param name="row"></param>         private void AddDashboardPriceToGridview(DashboardPrice item, GridViewRow row)         {             row.Cells[0].Text = Convert.ToString(item.Price);             row.Cells[1].Text = item.PriceName;             row.Cells[2].Text = item.Item;             row.Cells[3].Text = Convert.ToString(item.Bill);             if (item.Item.ToLower() == itemCode.ToLower()                 && item.Bill == billNo)             {                 if (item.PriceName == "MRP")                 {                     // Set MRP                     row.Cells[4].Text = Convert.ToString(item.Price);                 }                 // Set GP                 row.Cells[5].Text = "replace this with calculation";             }         }     }     /// <summary>     /// Data model class for the gridview row item     /// </summary>     public class DashboardPrice     {         public int Price { get; set; }         public string PriceName { ge

Категории

Добро пожаловать на сайт DraftingCode Q&A, где вы можете задавать вопросы и получать ответы от других членов сообщества.

Связанных вопросов не найдено

...