怎样在datagridview中添加Button列,实现修改,删除功能
有一个datagridview,希望在后面添加几个button列,实现每一行数据的修改和删除功能
修改时,点击修改按钮。在窗体的其它地方会有一些文本框,标签和一个button按钮。在文本框中修改后,点击button就可以修改。写回数据库和刷新datagridview
删除时,按下删除按钮就可以删除该行。同时数据库和表格更新。
我时刚刚开始做的,一点头绪都没有。希望给位帮帮忙。最好又完整的代码 ,小弟在这里谢谢了!
------解决方案--------------------在DATAGERIDVIEW自定义列,列类型为DataGridViewButtonColumn
------解决方案--------------------对DataGridView添加一个Column,对Column的类型选DataGridViewButtonColumn即可。
------解决方案--------------------
  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {
           //列的索引
           int x =  e.ColumnIndex;
             //行的索引
           int y = e.RowIndex 
       }
------解决方案--------------------学习,加接分。
------解决方案--------------------
可以在编辑列里上下移动改变列的顺序。
button后你就取到该行的ID,然后根据ID做你要做的事,比如删除,修改等等
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
   {
       string id = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
   }
这个就是选择所在行的ID
------解决方案--------------------加按钮列
如果数据源里将update\delete的sql语句都已经设置好了的话,按钮就能用了,都不用写代码
------解决方案--------------------<%@ Page language="C#" %>
<html>
 <body>
   <form runat="server">    
     <h3>CommandField Example</h3>
     <asp:gridview id="CustomersGridView"  
       datasourceid="CustomersSqlDataSource"  
       autogeneratecolumns="false"
       datakeynames="CustomerID" 
       runat="server">      
       <columns>
         <asp:commandfield showeditbutton="true"
           buttontype="Image"
           editimageurl="~\Images\EditButton.jpg"
           cancelimageurl="~\Images\CancelButton.jpg"
           updateimageurl="~\Images\UpdateButton.jpg"
           headertext="Edit Controls"/>
         <asp:boundfield datafield="CustomerID"
           headertext="Customer ID" />
         <asp:boundfield datafield="CompanyName"
           headertext="Company Name"/>
         <asp:boundfield datafield="Address"
           headertext="Address"/>
         <asp:boundfield datafield="City"
           headertext="City"/>
         <asp:boundfield datafield="PostalCode"
           headertext="ZIP Code"/>
         <asp:boundfield datafield="Country"
           headertext="Country"/>
       </columns>      
     </asp:gridview>          
     <!-- This example uses Microsoft SQL Server and connects  -->
     <!-- to the Northwind sample database. Use an ASP.NET     -->
     <!-- expression to retrieve the connection string value   -->
     <!-- from the Web.config file.                            -->
     <asp:sqldatasource id="CustomersSqlDataSource" 
       selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
       updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
       connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"