grid_rowcolor.aspx.cs 小问题

Mar 30, 2010 at 3:17 AM
Edited Apr 1, 2010 at 5:34 AM
//grid_rowcolor.aspx.cs 如果不在页面加载时BindGrid的话,而在Button1_Click时BindGrid 行并不能变颜色
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

namespace ExtAspNet.Examples.data
{
    public partial class grid_rowcolor : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               // BindGrid();
            }
        }

        #region BindGrid
        private void BindGrid()
        {
            DataTable table = IniGrid();

            Grid1.DataSource = table;
            Grid1.DataBind();
        }

        private static DataTable IniGrid()
        {
            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));
            DataColumn column5 = new DataColumn("MyCheckBox", typeof(bool));
            table.Columns.Add(column1);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Columns.Add(column5);

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

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

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

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

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

            return table;
        }

        #endregion

        #region Grid1

        protected void Grid1_PreRowDataBound(object sender, ExtAspNet.GridPreRowEventArgs e)
        {
            //ExtAspNet.LinkButtonField lbfAction1 = Grid1.FindColumn("lbfAction1") as ExtAspNet.LinkButtonField;
            //ExtAspNet.LinkButtonField lbfAction2 = Grid1.FindColumn("lbfAction2") as ExtAspNet.LinkButtonField;

            //if (e.RowIndex % 2 == 0)
            //{
            //    lbfAction1.Enabled = false;
            //    lbfAction2.Enabled = false;
            //}
            //else
            //{
            //    lbfAction1.Enabled = true;
            //    lbfAction2.Enabled = true;
            //}
        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            // DataItem是行的数据源,
            // 如果数据源是DataTable/DataView/DataSet,则e.DataItem是DataRow
            // 如果数据源是List<MyClass>,则e.DataItem是MyClass

            // e.Values 是实际赋予此行每列的值,可以修改

            ExtAspNet.WindowField windowField1 = Grid1.FindColumn("myWindowField") as ExtAspNet.WindowField;

            DataRow row = e.DataItem as DataRow;
            if (row != null)
            {
                e.Values[1] = String.Format("RowDataBound - {0}", row["MyText"]);

                //e.Values[5] = "<img src=\"/extaspnet/images/16/3.gif\" style=\"border-width: 0px;width:100px;\" ext:qtip=\"item3, I am a great programmer.\"/>";
                if (e.RowIndex % 2 == 0)
                {
                    highlightRows.Text += e.RowIndex.ToString() + ",";
                }
            }
        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "Action1" || e.CommandName == "Action2")
            {
                Alert.ShowInParent(String.Format("You click line: {0}, CommandName: {1}", e.RowIndex + 1, e.CommandName));
            }
        }

        #endregion

        #region Event

        /// <summary>
        /// 回发How many lines were selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
             BindGrid();
            
        }


        protected string GetMyValue(object value)
        {
            return (Convert.ToInt32(value) * 100).ToString();
        }


        #endregion

    }
}
另 如果单独打开 basic/login.aspx 然后迅速最小化,有机会遇到以下情况,“-WindowPosition默认居中 ”后才有这种情况,以前正常。

Coordinator
Apr 2, 2010 at 9:19 AM

这两个问题我会在下个版本发布时解决.....

Apr 2, 2010 at 9:56 AM

楼主英明啊。

Editor
Apr 7, 2010 at 3:23 PM

Sanshi, This feature and Grid.FindControl in TemplateField are for next version together.