将DataGrid升级到GridView的具体方法

 

将DataGrid升级到DataGrid的具体方法

 

1.      首先添加如下样式,这里只做了几个常用的,基本也够了,更多的样式需做进一步的设计,建议将这些样式放到一个公共的css文件中,如:WOStyle.css

.gvEditContainer

{

     overflow: auto;

     left: 0px;

     padding-right: 0px;

     padding-bottom: 0px;

     padding-left: 0px;

     padding-top: 0px;

     margin: 0px;

     border-right: 0px solid;

     border-top: 0px solid;

     border-left: 0px solid;

     border-bottom: 0px solid;

     vertical-align: super;

     scrollbar-face-color: #ffffff;

     scrollbar-highlight-color: #9e9e9e;

     scrollbar-shadow-color: #9e9e9e;

     scrollbar-3dlight-color: #ffffff;

     scrollbar-arrow-color: #9e9e9e;

     scrollbar-track-color: #eaeaea;

     scrollbar-darkshadow-color: #eaeaea;

     position: relative;

     z-index: 0;

}

.gvEdit

{

     background-color: White;

     border-color: #CCCCCC;

     border-width: 1px;

     border-style: none;

     padding: 0;

}

.gvEditHeader

{

     position: relative;

     left: expression(this.parentElement.scrollLeft);

     top: expression(this.parentElement.parentElement.parentElement.parentElement.scrollTop-3);

     z-index: 0;

     font-weight: bold;

     background-color: #006699;

     color: White;

     text-align: center;

}

.gvEditFooter

{

     color: White;

     background-color: SteelBlue;

}

 

2.      将主页面相关文件中的DataGrid改成GridView.

2.1修改文件XXX_Main.aspx

在head区域添加以下代码

  <script type="text/javascript" src="../../JS/SortTable.js"></script>

将XXX_Main.aspx文件中的BoundColumn全部替换成BoundField

示意如下:

<asp:GridView ID="gv" CssClass="sortable" UseAccessibleHeader="false" AutoGenerateColumns="False"

                                                            runat="server" Width="100%" OnRowDataBound="gv_RowDataBound">

                                                            <Columns>

<asp:BoundField DataField="AssetCode" ReadOnly="True" HeaderText="省类项目节"> </asp:BoundField>

<asp:BoundField DataField="Amount" ReadOnly="True" HeaderText="采购数量"></asp:BoundField>

 <asp:BoundField DataField="UnitPrice" ReadOnly="True" HeaderText="采购单价" DataFormatString="{0:N2}">

  </asp:BoundField>

<asp:BoundField DataField="RequestDate" ReadOnly="True" HeaderText="申请日期"></asp:BoundField>

                                                            </Columns>

                                                        </asp:GridView>

 

2.2修改文件XXX_Main.cs

将这一句

this.dg.DataSource = this.WOdvDetail;

 

换成

if (this.WOdvDetail.Count > 0)

            {

                this.gv.DataSource = this.WOdvDetail;

            }

            else

            {

                DataView dvTemp = this.WOdvDetail;

                dvTemp.AddNew();

                this.gv.DataSource = dvTemp;

            }

 

将下面这段

protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)

        {

            UIUtil.DataGridSetColAdjust((DataGrid)sender, e);

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

            {

                //这里省略。。。

            }

        }

换成:

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)

        {

                     //注意这一句不要了,因为已经通过界面就实现了该功能,

//而且该方法不支持GridView,如果需要,可以请陈工改进。

            //UIUtil.DataGridSetColAdjust((DataGrid)sender, e);

 

            if (this.WOdvDetail.Count == 0)

            {

                if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)

                {

                    e.Row.Visible = false;

                }

                return;

            }

 

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                 //这里省略。。。

            }

        }

 

3.      将明细编辑相关文件中的DataGrid改成GridView.

3.1修改文件XXX_DetailEdit.aspx

将DataGrid改成GridView:

示意如下,”复制”栏位之前的前面部分基本相同,后面的自行参考:

<div id="sp" class="gvEditContainer" runat="server" style="width: 100%; height: 450px;">

        <asp:GridView ID="gv" CssClass="gvEdit" UseAccessibleHeader="false" AutoGenerateColumns="False"

            runat="server" Width="220%" OnRowCreated="gv_RowCreated" OnRowDataBound="gv_RowDataBound"

            OnRowCommand="gv_RowCommand">

           <HeaderStyle  CssClass="gvEditHeader"/>

            <AlternatingRowStyle CssClass="gvAlternatingRow" />

            <Columns>

                <asp:TemplateField HeaderText="行号">

                    <ItemStyle Width="40px" HorizontalAlign="Center"></ItemStyle>

                    <ItemTemplate>

                        <%# Container.DataItemIndex+1 %></ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="选择">

                    <HeaderStyle Width="50px"></HeaderStyle>

                    <ItemStyle Width="50px" HorizontalAlign="Center"></ItemStyle>

                    <HeaderTemplate>

                        <asp:CheckBox ID="chkAll" runat="server" ToolTip="全选/全不选" AutoPostBack="false"></asp:CheckBox>选择

                    </HeaderTemplate>

                    <ItemTemplate>

                        <asp:CheckBox ID="chkSelect" runat="server" ToolTip="选择" AutoPostBack="false"></asp:CheckBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="复制">

                    <HeaderStyle Width="40px"></HeaderStyle>

                    <ItemStyle Width="40px" HorizontalAlign="Center"></ItemStyle>

                    <ItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" Text="复制" CommandName="Copy" CausesValidation="false"></asp:LinkButton>

                    </ItemTemplate>

                </asp:TemplateField>

              

                <asp:TemplateField HeaderText="省类项目节*">

                    <HeaderStyle Width="120px"></HeaderStyle>

                    <ItemStyle Width="120px" HorizontalAlign="Left"></ItemStyle>

                    <HeaderTemplate>

                        类项目节<span style="color: Red;">*</span>

                    </HeaderTemplate>

                    <ItemTemplate>

                        <table cellspacing="0" cellpadding="0" width="100%" border="0">

                            <tr>

                                <td width="98%">

                                    <asp:Label ID="lbAssetCode" runat="server" Width="100%" Text='<%# DataBinder.Eval(Container, "DataItem.AssetCode") %>'>

                                    </asp:Label>

                                </td>

                                <td>

                                    <asp:LinkButton ID="lkAssetCode" runat="server" BorderWidth="1px" BorderStyle="None"

                                        CommandName="assetcode|0">

                                                        <img src="../../image/OptionDG.gif" border="0" alt="选择或修改“资产目录编码和资产名称”。" />

                                    </asp:LinkButton>

                                </td>

                                <td>

                                    <asp:LinkButton ID="lbAssetCodeClear" runat="server" BorderWidth="1px" BorderStyle="None"

                                        CommandName="assetcode|C" Visible="false">

                                                        <img src="../../image/ClearDG.gif" border="0" alt="清除“资产目录编码和资产名称”。" />

                                    </asp:LinkButton>

                                </td>

                            </tr>

                        </table>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField DataField="AssetName" ReadOnly="True" HeaderText="省资产名称">

                    <HeaderStyle Width="90px"></HeaderStyle>

                    <ItemStyle Width="90px" HorizontalAlign="Left"></ItemStyle>

                </asp:BoundField>

             

                <asp:TemplateField HeaderText="数量*">

                    <HeaderStyle Width="45px"></HeaderStyle>

                    <ItemStyle Width="45px" HorizontalAlign="Right"></ItemStyle>

                    <HeaderTemplate>

                        数量<span style="color: Red;">*</span>

                    </HeaderTemplate>

                    <ItemTemplate>

                        <nb:NumberBox ID="nbAmount" runat="server" Width="100%" ValidationRegularExpression="[0-9]*([.]|[0-9]){0,1}[0-9]{0,2}$"

                            Text='<%# DataBinder.Eval(Container, "DataItem.Amount") %>' AllowNegatives="False"

                            DecimalPlaces="0" DisplayZERO="False" Value="0" DecimalSymbol=".">

                       

                        </nb:NumberBox>

                    </ItemTemplate>

                </asp:TemplateField>

         

                <asp:TemplateField HeaderText="申请日期*">

                    <HeaderStyle Width="100px"></HeaderStyle>

                    <ItemStyle Width="100px" HorizontalAlign="Left"></ItemStyle>

                    <HeaderTemplate>

                        申请日期<span style="color: Red;">*</span>

                    </HeaderTemplate>

                    <ItemTemplate>

                        <dp:DatePicker ID="tbRequestDate" runat="server" Width="80%" ValidationRegularExpression="^((((1[6-9]|[2-9]/d)/d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]/d|3[01]))|(((1[6-9]|[2-9]/d)/d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]/d|30))|(((1[6-9]|[2-9]/d)/d{2})-0?2-(0?[1-9]|1/d|2[0-8]))|(((1[6-9]|[2-9]/d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"

                            IsNull="True" Text='<%# DataBinder.Eval(Container, "DataItem.RequestDate") %>'

                            ImgUrl="../../IMAGE/calendar.JPG">

                      

                        </dp:DatePicker>

                    </ItemTemplate>

                </asp:TemplateField>

             </Columns>

        </asp:GridView>

 

3.2 修改文件XXX_DetailEdit.cs

将这一句

dg.DataSource = WOdvDetail;

换成

if (WOdvDetail.Count > 0)

            {

                this.gv.DataSource = WOdvDetail;

            }

            else

            {

                DataView dvTemp = WOdvDetail;

                dvTemp.AddNew();

                this.gv.DataSource = dvTemp;

            }

 

将这一段

protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)

        {

            UIUtil.DataGridSetKeepHeaderVisible((DataGrid)sender, e);

            UIUtil.DataGridSetColAdjust((DataGrid)sender, e);

 

            if (e.Item.ItemType == ListItemType.Footer)

            {

                var chkAll = (CheckBox)e.Item.FindControl("chkAll");

 

                chkAll.Attributes.Add(HtmlTextWriterAttribute.Onclick.ToString(), "javascript:SelectAll(this);");

            }

 

        }

换成

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)

        {

                     //下面这两句都不要了,请删除,相应的功能也都有实现。

                     //UIUtil.DataGridSetKeepHeaderVisible((DataGrid)sender, e);

            //UIUtil.DataGridSetColAdjust((DataGrid)sender, e);

 

            if (e.Row.RowType == DataControlRowType.Header)

            {

                var chkAll = (CheckBox)e.Row.FindControl("chkAll");

 

                chkAll.Attributes.Add(HtmlTextWriterAttribute.Onclick.ToString(), "javascript:SelectAll(this);");

            }

            if (this.WOdvDetail.Count == 0)

            {

                if (e.Row.RowType == DataControlRowType.DataRow)

                {

                    e.Row.Visible = false;

                }

            

            }

        }

 

将之前的dg_DeleteCommand事件换成 gv_RowCommand事件,之前如果有用dg_SelectedIndexChanged事件来做选择的,也请一并放入到gv_RowCommand事件中来实现。

修改后的代码示意如下:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            Control c = ((Control)e.CommandSource);

            GridViewRow gvr = (GridViewRow)c.Parent.Parent;

            int currentIndex = gvr.RowIndex;

            string[] cmdName = e.CommandName.ToLower().Split('|');

            if (cmdName[0] == "copy")

            {

                RowCopy(currentIndex);

                ShowData();

                return;

            }

 

            ViewState.Add("Event", string.Format("{0}|{1}", currentIndex, cmdName[0]));

            string title = string.Empty;

            ListItem[] listItem;

            DataRow drowL = WOdvDetail[currentIndex].Row;

 

            #region 删除

            if (cmdName.Length > 1)

            {

                if (cmdName[1] == "c")

                {

                    if (cmdName[0] == "permcode")

                    {

                        drowL[cmdName[0]] = DBNull.Value;

                        drowL["permName"] = DBNull.Value;

                    }

                    else

                    {

                        drowL[cmdName[0]] = DBNull.Value;

                        drowL[cmdName[0] + "Name"] = DBNull.Value;

                    }

 

                    ShowData();

                    return;

                }

            }

            #endregion

 

            #region 选择

            switch (cmdName[0])

            {

                case "labor":

                    #region 选择收货人

                    title = "收货人";

                    listItem = blSrv.GetListArrayForTextAndValue("dbo.View_Labor_Active", "Labor", "LaborId",

                                           string.Empty);

                    if (listItem == null)

                    {

                        Alert("注意:检索 " + title + " 名称数据出错!");

                    }

                    else

                    {

                        Session["_ListSource"] = listItem;

                        cwhSrv.ShowListSelectDialog(this, WOFormName, WOAction, title, false, false);

                    }

                    #endregion

                    break;

             

                case "useman":

                    #region 选择使用人

                    title = "使用人";

                    listItem = blSrv.GetListArrayForTextAndValue("dbo.View_Labor_Active", "Labor", "LaborId",

                                                               "type='职员'");

                    if (listItem == null)

                    {

                        Alert("注意:检索 " + title + " 名称数据出错!");

                    }

                    else

                    {

                        Session["_ListSource"] = listItem;

                        cwhSrv.ShowListSelectDialog(this, WOFormName, WOAction, title, false, false);

                    }

                    #endregion

                    break;

            }

            #endregion

        }

 

 

修改事件Page_Load中的代码

示意如下:

protected override void Page_Load(object sender, EventArgs e)

        {

            base.Page_Load(sender, e);

            KeepScrollPosition(this.sp);

            if (IsPostBack)

            {

                ReceiveData();

            }

            else

            {

                //初始化...

                PageInit();

                ShowData();

            }

 

            if (Session["thingNumber"] != null || Session["thingName"] != null)

            {

                string s = ViewState["Event"].ToString();//rowIndex|ComamndName[0]

                ViewState.Remove("Event");

                string[] s0 = s.Split('|');

                int rowIndex = int.Parse(s0[0]);//rowIndex

                string fieldName = s0[1];//ComamndName[0]

 

                DataRowView drv = WOdvDetail[rowIndex];

                if (drv == null)

                {

                    Alert("注意:参数丢失,找不到相应的行记录。");

                    return;

                }

 

                if (fieldName == "labor" || fieldName == "requestman" || fieldName == "purchaseman")

                {

                    drv[fieldName] = Session["thingNumber"].ToString();

                    drv[fieldName + "Name"] = Session["thingName"].ToString();

                }

 

                if (fieldName == "assetcode")

                {

                    string[] assetCatalog = Session["thingNumber"].ToString().Split('|');

 

                    if (WOdvDetail[rowIndex]["AssetCode"].ToString() != assetCatalog[0] || WOdvDetail[rowIndex]["AssetName"].ToString() != assetCatalog[1])

                    {

                        WOdvDetail[rowIndex]["AssetCode"] = assetCatalog[0];

                        WOdvDetail[rowIndex]["AssetName"] = assetCatalog[1];

 

                        //修改集团资产目录

                        string[] fields = new string[] { " [PermName] as Val" };

                        string where = string.Format("(Code='{0}' And AssetName='{1}')", assetCatalog[0], assetCatalog[1]);

                        DataTable dt = this.dbSrv.GetRecordSetByCondition("dbo.tblAssetCatalog2PermanentAssetCatalog", fields, where);

                        if (dt == null || dt.Rows.Count == 0)

                        {

                            this.WOdvDetail[rowIndex]["PermName"] = DBNull.Value;

                        }

                        else

                        {

                            this.WOdvDetail[rowIndex]["PermName"] = dt.Rows[0][0].ToString();

                        }

                    }

                }

 

                Session["thingNumber"] = null;

                Session["thingName"] = null;

                ShowData();

            }

 

            if (Session["DPO_Action"] != null)

            {

                ShowData();

                Session.Remove("DPO_Action");

            }

 

            #region 导入操作

            if (Session["ImportDataList"] != null)

            {

                //省略。。。

            }

            #endregion

        }

4.其他说明

其他的一些小修改,就不多说了:

如:

需要将

DataGridItem

换成

GridViewRow

if (dgi.ItemType == ListItemType.Item || dgi.ItemType == ListItemType.AlternatingItem)

换成

if (dgi.RowType == DataControlRowType.DataRow)

 

最后一点建议:

1.不要在明细编辑页面每一条记录都加一个删除按钮。

推荐的方式是,先选择,然后通过一个按钮统一删除所选。

2.全选按钮可以做在表头,而且有复选框,具体可以参考前面的修改方法

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值