一次更新Datagrid 中多列数据

转载 2007年09月29日 17:07:00
 
在datagrid中一般我们只需一列列的更新数据,但有时需一次更新所有列中数据,提高数据编辑效率。
为了方便演示用xml文件做为数据源
xml文件:(UserInfo.xml)
<?xmlversion="1.0"standalone="yes"?>
<UserInfos>
 <UserInfo>
    <Email>some@you.net</Email>
    <Name>me</Name>
    <TEL>0123456</TEL>
 </UserInfo>
 <UserInfo>
    <Email>youraddress@you.com</Email>
    <Name>he</Name>
    <TEL>7894563</TEL>
 </UserInfo>
 <UserInfo>
    <Email>youraddress@you.com</Email>
    <Name>222</Name>
    <TEL>123342</TEL>
 </UserInfo>
 <UserInfo>
    <Email>youraddress@you.com</Email>
    <Name>haha</Name>
    <TEL>3545445</TEL>
 </UserInfo>
   <UserInfo>
    <Email>youraddress@you.com</Email>
    <Name>sam</Name>
    <TEL>879563</TEL>
 </UserInfo>
</UserInfos>
页面代码:
<form id="Form1" method="post" runat="server">
                     <asp:DataGrid id="DGrid" runat="server" ShowFooter="True" GridLines="None" CellPadding="2" AutoGenerateColumns="False">
                            <HeaderStyle Font-Bold="True"></HeaderStyle>
                            <Columns>
                                   <asp:TemplateColumn HeaderText="名字">
                                          <ItemTemplate>
                                                 <asp:TextBox id=Name runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'>
                                                 </asp:TextBox>
                                          </ItemTemplate>
                                          <FooterTemplate>
                                                 <asp:TextBox id="NewName" runat="server" Width="109px"></asp:TextBox>
                                          </FooterTemplate>
                                   </asp:TemplateColumn>
                                   <asp:TemplateColumn HeaderText="电话">
                                          <ItemTemplate>
                                                 <asp:TextBox id=TEL runat="server" Width="109px" Text='<%# DataBinder.Eval(Container, "DataItem.TEL") %>'>
                                                 </asp:TextBox>
                                          </ItemTemplate>
                                          <FooterTemplate>
                                                 <asp:TextBox id="NewTEL" runat="server" Width="109px"></asp:TextBox>
                                          </FooterTemplate>
                                   </asp:TemplateColumn>
                                   <asp:TemplateColumn HeaderText="Email">
                                          <ItemTemplate>
                                                 <asp:TextBox id=Email runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'>
                                                 </asp:TextBox>
                                          </ItemTemplate>
                                          <FooterTemplate>
                                                 <asp:TextBox id="NewEmail" runat="server"></asp:TextBox>
                                          </FooterTemplate>
                                   </asp:TemplateColumn>
                                   <asp:TemplateColumn HeaderText="删除">
                                          <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                          <ItemTemplate>
                                                 <asp:CheckBox Runat="server" ID="chkDelete"></asp:CheckBox>
                                          </ItemTemplate>
                                          <FooterStyle HorizontalAlign="Center"></FooterStyle>
                                          <FooterTemplate>
                                                 <asp:LinkButton Runat="server" Text="增加" CommandName="Add" ID="Linkbutton1"></asp:LinkButton>
                                          </FooterTemplate>
                                   </asp:TemplateColumn>
                            </Columns>
                     </asp:DataGrid>
                     <asp:Button id="btnUpdate" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 208px" runat="server"
                            Text="更新"></asp:Button>
              </form>
后台代码:
         private DataSet dsUser;//定义dsUser
         private void Page_Load(object sender, System.EventArgs e) 
         { 
              // 读取XML 文件.到dataset中
              dsUser = new DataSet(); 
              dsUser.ReadXml(Server.MapPath("UserInfo.xml")); 
              if (!Page.IsPostBack ) 
              {  
                   BindData();  
              } 
         } 
         private void BindData() 
         { 
              // datagrid绑定dataset
              DataView dv = new DataView(dsUser.Tables["UserInfo"]); 
              DGrid.DataSource = dv; 
              DGrid.DataBind(); 
         } 
        //把dataset保存到xml文件
         private void Save() 
         { 
              dsUser.WriteXml(Server.MapPath("UserInfo.xml")); 
         }  
         private void DGrid_ItemCommand(object source , System.Web.UI.WebControls.DataGridCommandEventArgs e) 
         {   
              //增加一行到dataset中。使用了arrary结构
              if (e.CommandName == "Add") 
              { 
                   string[] sUser = {"", "", ""}; 
                   sUser[0] = ((TextBox)e.Item.FindControl("NewEmail")).Text; 
                   sUser[1] = ((TextBox)e.Item.FindControl("NewName")).Text; 
                   sUser[2] = ((TextBox)e.Item.FindControl("NewTEL")).Text; 
 
                   dsUser.Tables["UserInfo"].Rows.Add(sUser); 
 
                   Save(); 
              } 
 
              BindData(); 
         }  
         private void btnUpdate_Click(object sender, System.EventArgs e) 
         { 
              // 遍历datagrid中所有列. 
              foreach (DataGridItem di in DGrid.Items) 
              { 
                   //不遍历datagrid中的 header or footer.  
                   if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem) 
                   { 
 
                       DataRow dr = dsUser.Tables["UserInfo"].Rows.Find(DGrid.DataKeys[di.ItemIndex]); 
 
                       // 如果删除被选中. 
                       if (((CheckBox)di.FindControl("chkDelete")).Checked) 
                       { 
                            dsUser.Tables["UserInfo"].Rows.Remove(dr); 
                       } 
                       else 
                       { 
                            // 更新
                            dr["Email"] = ((TextBox)di.FindControl("Email")).Text; 
                            dr["Name"] = ((TextBox)di.FindControl("Name")).Text; 
                            dr["TEL"] = ((TextBox)di.FindControl("TEL")).Text; 
                       } 
                   } 
              } 
 
              // 如果有改动
              if (dsUser.HasChanges()) 
              { 
                   Save(); 
              } 
 
              BindData(); 
  }

相关文章推荐

EASYUI DATAGRID 多列复选框CheckBox

function initTableGMAL() { $("#dg_gmal").datagrid({ url: "/Manager/PublishManage...
  • pnf88
  • pnf88
  • 2015-05-02 00:48
  • 374

Flex4中DataGrid,itemrender 多行多列值控制

写了一个简单的Demo,目的是帮助大家初步了解itemrender的使用,以及如何对多行数据同时操作。 Demo操作过程。 1. DataGrid为3列,第一列是check box. 2...

Flex 4.5 Spark DataGrid 多列排序

package { import mx.collections.ICollectionView; import mx.collections.IList; import mx.collectio...

DataGrid更新数据时的一系列问题的解决

【问题描述】一个表格,里面是员工的信息,可以通过update操作更新员工信息,但这一操作需要在表格下的表单中进行。如上图所示,必须先按下update,然后在红色方框的表单中修改。现在希望能直接在表格里...

Combox 加载多列数据

  • 2011-12-29 07:50
  • 184KB
  • 下载

关于CListCtrl控件更新Item的闪烁问题和一次插入大容量数据的显示问题解决办法

1.使用SetRedraw禁止窗口重绘,操作完成后,再恢复窗口重绘 m_ctlList.SetRedraw(FALSE); //以下为更新数据操作 //…… //恢复窗口重绘 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)