首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

GridView批量更新,合并了三列遇到的有关问题

2012-10-12 
GridView批量更新,合并了三列遇到的问题。----根据http://msdn.microsoft.com/zh-cn/library/aa992036(vVS

GridView批量更新,合并了三列遇到的问题。
----根据http://msdn.microsoft.com/zh-cn/library/aa992036(v=VS.100).aspx?ppud=4 演练:GridView批量更新的示例,先将各列转换为 模板,再编写后台代码。 


----我对自己的数据库做了相同的尝试,没有问题。但我后来把SQLDataSource中的三列合并起来(原来分别是:[PropertyCityReceivable], [PropertyRoadReceivable], [PropertyAddressReceivable] 后来合并改成 [PropertyCityReceivable]+ [PropertyRoadReceivable]+ [PropertyAddressReceivable])。  

----

----问题出来了!我每次点击Button更新数据,Columnn1列(也就是[PropertyCityReceivable]+ [PropertyRoadReceivable]+ [PropertyAddressReceivable])的数据就自动消失了。(这三列如果不合并就不会消失,一合并就会消失)。

---- 

----我感觉这应该是个简单的问题,但我就是找不出问题所在。很纠结,请各位不吝赐教,谢谢!!!以下分别是前台和后台完整的代码。注释掉的部分是《演练》中的示例  

------------------------------- 前台--------------------------  

HTML code
//篇幅所限, 删除前面的代码                        <Columns>                            <asp:TemplateField HeaderText="ReceivableID" SortExpression="ReceivableID">                                <EditItemTemplate>                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ReceivableID") %>'></asp:TextBox>                                </EditItemTemplate>                                <ItemTemplate>                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ReceivableID") %>'></asp:Label>                                </ItemTemplate>                            </asp:TemplateField>                            <asp:BoundField DataField="PropertyIDReceivable"                                 HeaderText="PropertyIDReceivable" ReadOnly="True"                                 SortExpression="PropertyIDReceivable" />                            <asp:TemplateField HeaderText="ContractNumberReceivable"                                 SortExpression="ContractNumberReceivable">                                <EditItemTemplate>                                    <asp:TextBox ID="TextBox2" runat="server"                                         Text='<%# Bind("ContractNumberReceivable") %>'></asp:TextBox>                                </EditItemTemplate>                                <ItemTemplate>                                    <asp:Label ID="Label2" runat="server"                                         Text='<%# Bind("ContractNumberReceivable") %>'></asp:Label>                                </ItemTemplate>                            </asp:TemplateField>                            <asp:TemplateField HeaderText="ManageProjectReceivable"                                 SortExpression="ManageProjectReceivable">                                <EditItemTemplate>                                    <asp:TextBox ID="TextBox3" runat="server"                                         Text='<%# Bind("ManageProjectReceivable") %>'></asp:TextBox>                                </EditItemTemplate>                                <ItemTemplate>                                    <asp:TextBox ID="TextBox_ManageProjectReceivable" runat="server"                                         Text='<%# Bind("ManageProjectReceivable") %>'></asp:TextBox>                                </ItemTemplate>                            </asp:TemplateField>                            <asp:TemplateField HeaderText="Column1" SortExpression="Column1">                                <EditItemTemplate>                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Column1") %>'></asp:Label>                                </EditItemTemplate>                                <ItemTemplate>                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Column1") %>'></asp:Label>                                </ItemTemplate>                            </asp:TemplateField>                        </Columns>                                            </asp:GridView>                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"                         ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>"                         DeleteCommand="DELETE FROM [ReceivableTable] WHERE [PropertyIDReceivable] = @PropertyIDReceivable"                         InsertCommand="INSERT INTO [ReceivableTable] ([ReceivableID], [PropertyIDReceivable], [ContractNumberReceivable], [ManageProjectReceivable], [PropertyCityReceivable], [PropertyRoadReceivable], [PropertyAddressReceivable]) VALUES (@ReceivableID, @PropertyIDReceivable, @ContractNumberReceivable, @ManageProjectReceivable, @PropertyCityReceivable, @PropertyRoadReceivable, @PropertyAddressReceivable)"                         SelectCommand="SELECT [ReceivableID], [PropertyIDReceivable], [ContractNumberReceivable], [ManageProjectReceivable], [PropertyCityReceivable]+ [PropertyRoadReceivable]+ [PropertyAddressReceivable] FROM [ReceivableTable]"                         UpdateCommand="UPDATE [ReceivableTable] SET [ReceivableID] = @ReceivableID, [ContractNumberReceivable] = @ContractNumberReceivable, [ManageProjectReceivable] = @ManageProjectReceivable, [PropertyCityReceivable] = @PropertyCityReceivable, [PropertyRoadReceivable] = @PropertyRoadReceivable, [PropertyAddressReceivable] = @PropertyAddressReceivable WHERE [PropertyIDReceivable] = @PropertyIDReceivable">                        <DeleteParameters>                            <asp:Parameter Name="PropertyIDReceivable" Type="Int32" />                        </DeleteParameters>                        <UpdateParameters>                            <asp:Parameter Name="ReceivableID" Type="Int32" />                            <asp:Parameter Name="ContractNumberReceivable" Type="String" />                            <asp:Parameter Name="ManageProjectReceivable" Type="String" />                            <asp:Parameter Name="PropertyCityReceivable" Type="String" />                            <asp:Parameter Name="PropertyRoadReceivable" Type="String" />                            <asp:Parameter Name="PropertyAddressReceivable" Type="String" />                            <asp:Parameter Name="PropertyIDReceivable" Type="Int32" />                        </UpdateParameters>                        <InsertParameters>                            <asp:Parameter Name="ReceivableID" Type="Int32" />                            <asp:Parameter Name="PropertyIDReceivable" Type="Int32" />                            <asp:Parameter Name="ContractNumberReceivable" Type="String" />                            <asp:Parameter Name="ManageProjectReceivable" Type="String" />                            <asp:Parameter Name="PropertyCityReceivable" Type="String" />                            <asp:Parameter Name="PropertyRoadReceivable" Type="String" />                            <asp:Parameter Name="PropertyAddressReceivable" Type="String" />                        </InsertParameters>                    </asp:SqlDataSource>               -----------------------后台代码[b](注释的部分是《演练》中的示例)[/b]-------------[code=C#]//篇幅所限, 删除命名空间public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {    }    private bool tableCopied = false;    private DataTable originalDataTable;    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        if (e.Row.RowType == DataControlRowType.DataRow)            if (!tableCopied)            {                originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();                ViewState["originalValuesDataTable"] = originalDataTable;                tableCopied = true;            }    }    protected void UpdateButton_Click(object sender, EventArgs e)    {        originalDataTable = (DataTable)ViewState["originalValuesDataTable"];        foreach (GridViewRow r in GridView1.Rows)            if (IsRowModified(r)) { GridView1.UpdateRow(r.RowIndex, false); }        // Rebind the Grid to repopulate the original values table.        tableCopied = false;        GridView1.DataBind();    }    protected bool IsRowModified(GridViewRow r)    {        //int currentID;        //string currentTitleOfCourtesy;        //string currentLastName;        //string currentFirstName;        //string currentTitle;        //string currentExtension;        int currentID;        string currentManageProjectReceivable;        //currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);        currentID = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);        //currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;        //currentLastName = ((TextBox)r.FindControl("LastNameTextBox")).Text;        //currentFirstName = ((TextBox)r.FindControl("FirstNameTextBox")).Text;        //currentTitle = ((TextBox)r.FindControl("TitleTextBox")).Text;        //currentExtension = ((TextBox)r.FindControl("ExtensionTextBox")).Text;        currentManageProjectReceivable = ((TextBox)r.FindControl("TextBox_ManageProjectReceivable")).Text;        Response.Write(currentID);        //DataRow row =originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0];        DataRow row = originalDataTable.Select(string.Format("PropertyIDReceivable={0}", currentID))[0];        //if (!currentTitleOfCourtesy.Equals(row["TitleOfCourtesy"].ToString())) { return true; }        //if (!currentLastName.Equals(row["LastName"].ToString())) { return true; }        //if (!currentFirstName.Equals(row["FirstName"].ToString())) { return true; }        //if (!currentTitle.Equals(row["Title"].ToString())) { return true; }        //if (!currentExtension.Equals(row["Extension"].ToString())) { return true; }        if (!currentManageProjectReceivable.Equals(row["ManageProjectReceivable"].ToString())) { return true; }         return false;    }} 


[/code]

[解决办法]
你的篇幅太长了看的累死了,请问你是不是想这样呢?如果是就看看把:
你想在GridView中绑定一列(ABC)而原来是 A B C三列,如果你想这样绑定,建议你在你的Molde对应下的实体类中添加一个字段String类型Test字段,然后让Test=A+B+C 在前端GridView中显示这个字段就行了

热点排行