extaspnet grid 如何添加Columns

Nov 21, 2009 at 2:02 AM

ExtAspNet.BoundField bf = new ExtAspNet.BoundField();
bf.HeaderText = headtexts[i];
bf.DataField = cols[i];
bf.DataTooltipField = cols[i];
bf.ColumnId = cols[i];
Grid1.Columns.Add(bf);
这样就可以动态的往Grid里面添加列,但是这种动态添加的列,再插入数据时会发现,后面的每一列都跟第一列里面的数据一样,为什么,那如何动态增加列??

Coordinator
Nov 21, 2009 at 3:45 AM
Edited Nov 21, 2009 at 4:47 AM

写个简单的例子撒,这样的代码我没法看出你真正想做什么。

sanshi.ustc@gmail.com

 

Nov 22, 2009 at 2:24 AM

TestGrid.aspx的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestGrid.aspx.cs" Inherits="Web.TestGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ext:PageManager ID="PageManager1" runat="server" />
        <ext:Grid ID="Grid1" runat="server" EnableCheckBoxSelect="true" EnableRowNumber="true"
            Title="Grid">
            <Columns>
            </Columns>
        </ext:Grid>
    </div>
    </form>
</body>
</html>

 

TestGrid.aspx.cs的代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Web
{
    public partial class TestGrid : System.Web.UI.Page
    {
        #region LoadData
        private void LoadData()
        {
            DataTable table = GetTable();
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        private void InitGrid()
        {
            ExtAspNet.BoundField m_BoundField1 = new ExtAspNet.BoundField();
            m_BoundField1.DataTooltipField = "Id";
            m_BoundField1.DataField = "Id";
            m_BoundField1.DataFormatString = "{0}";
            m_BoundField1.HeaderText = "Id";
            Grid1.Columns.Add(m_BoundField1);

            ExtAspNet.BoundField m_BoundField2 = new ExtAspNet.BoundField();
            m_BoundField2.DataTooltipField = "MyText";
            m_BoundField2.DataField = "MyText";
            m_BoundField2.DataFormatString = "{0}";
            m_BoundField2.HeaderText = "MyText";
            Grid1.Columns.Add(m_BoundField2);


            ExtAspNet.BoundField m_BoundField3 = new ExtAspNet.BoundField();
            m_BoundField3.DataTooltipField = "MyValue";
            m_BoundField3.DataField = "MyValue";
            m_BoundField3.DataFormatString = "{0}";
            m_BoundField3.HeaderText = "MyValue";
            Grid1.Columns.Add(m_BoundField3);


            ExtAspNet.BoundField m_BoundField4 = new ExtAspNet.BoundField();
            m_BoundField4.DataTooltipField = "Year";
            m_BoundField4.DataField = "Year";
            m_BoundField4.DataFormatString = "{0}";
            m_BoundField4.HeaderText = "Year";
            Grid1.Columns.Add(m_BoundField4);
        }
        private DataTable GetTable()
        {
            DataTable table = new DataTable();
            DataColumn column1 = new DataColumn("Id", typeof(int));
            DataColumn column2 = new DataColumn("MyText", typeof(String));
            DataColumn column3 = new DataColumn("MyValue", typeof(String));
            DataColumn column4 = new DataColumn("Year", typeof(String));
            table.Columns.Add(column1);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);

            DataRow row = table.NewRow();
            row["Id"] = 101;
            row["MyText"] = "item1";
            row["MyValue"] = "1";
            row["Year"] = "2008";
            table.Rows.Add(row);

            row = table.NewRow();
            row["Id"] = 102;
            row["MyText"] = "item2";
            row["MyValue"] = "2";
            row["Year"] = "2007";
            table.Rows.Add(row);

            row = table.NewRow();
            row["Id"] = 103;
            row["MyText"] = "item3, I am a great programmer.";
            row["MyValue"] = "3";
            row["Year"] = "2006";
            table.Rows.Add(row);

            row = table.NewRow();
            row["Id"] = 104;
            row["MyText"] = "item4。";
            row["MyValue"] = "4";
            row["Year"] = "2005";
            table.Rows.Add(row);

            row = table.NewRow();
            row["Id"] = 105;
            row["MyText"] = "item5";
            row["MyValue"] = "5";
            row["Year"] = "2004";
            table.Rows.Add(row);
            return table;
        }

        #endregion

        protected void Page_Init(object sender, EventArgs e)
        {
            InitGrid();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}

运行后的结果如下:

Grid
 
Id
MyText
MyValue
Year
1
 
101
101
101
101
2
 
102
102
102
102
3
 
103
103
103
103
4
 
104
104
104
104
5
 
105
105
105
105
Coordinator
Nov 22, 2009 at 3:37 AM

Thanks gxpan.

这是Grid的一个BUG,我马上会更新Grid的实现代码并Checkin到SVN。

BTW:你的实现没有任何问题。有个小建议,你可以为m_BoundField2 设置ExpandUnusedSpace = true; 这样整个Grid就能被展开了。

Coordinator
Nov 22, 2009 at 3:46 AM

Check this version: 40116

 

Nov 22, 2009 at 4:00 AM

非常感谢

Nov 22, 2009 at 6:26 AM

Check this version: 40116

 

下后还不行啊,是不是没有处理到?

Nov 22, 2009 at 11:38 AM

已解决,非常感谢