在gridview中编辑行

时间:2011-01-18 02:38:44

标签: c# asp.net

我想帮助我处理我的代码。我有2个网格视图。在第一个gridview中,用户可以选择他想要的每一行复选框。这些行在第二个gridview中传输。所有这些我的代码都很好。现在,我想编辑第二个gridview中的数量列来更改值,但我不知道我必须在编辑框中写什么。 这是我的代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class ShowLand : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            BindPrimaryGrid();
            BindSecondaryGrid();

        }
    }
    private void BindPrimaryGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        string query = "select * from Land";
        SqlConnection con = new SqlConnection(constr);
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        gridview2.DataSource = dt;
        gridview2.DataBind();
    }



    private void GetData()
    {
        DataTable dt;
        if (ViewState["SelectedRecords1"] != null)
            dt = (DataTable)ViewState["SelectedRecords1"];
        else
            dt = CreateDataTable();
        CheckBox chkAll = (CheckBox)gridview2.HeaderRow
                            .Cells[0].FindControl("chkAll");
        for (int i = 0; i < gridview2.Rows.Count; i++)
        {
            if (chkAll.Checked)
            {
                dt = AddRow(gridview2.Rows[i], dt);
            }
            else
            {
                CheckBox chk = (CheckBox)gridview2.Rows[i]
                                .Cells[0].FindControl("chk");
                if (chk.Checked)
                {
                    dt = AddRow(gridview2.Rows[i], dt);
                }
                else
                {
                    dt = RemoveRow(gridview2.Rows[i], dt);
                }
            }
        }
        ViewState["SelectedRecords1"] = dt;
    }
    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gridview2.HeaderRow.Cells[0].FindControl("chkAll");
        chkAll.Checked = true;
        if (ViewState["SelectedRecords1"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRecords1"];
            for (int i = 0; i < gridview2.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gridview2.Rows[i].Cells[0].FindControl("chk");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("id = '" + gridview2.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0;
                    if (!chk.Checked)
                    {
                        chkAll.Checked = false;
                    }
                }
            }
        }
    }
            private DataTable CreateDataTable()
                {
                     DataTable dt = new DataTable();
                     dt.Columns.Add("id");
                     dt.Columns.Add("name");
                     dt.Columns.Add("price");
                     dt.Columns.Add("quantity");
                     dt.Columns.Add("total");
                     dt.AcceptChanges();
                     return dt;
                }
    private DataTable AddRow(GridViewRow gvRow, DataTable dt)
     {
    DataRow[] dr = dt.Select("id = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length <= 0)
    {
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1]["id"] = gvRow.Cells[1].Text;
        dt.Rows[dt.Rows.Count - 1]["name"] = gvRow.Cells[2].Text;
        dt.Rows[dt.Rows.Count - 1]["price"] = gvRow.Cells[3].Text;
        dt.Rows[dt.Rows.Count - 1]["quantity"] = gvRow.Cells[4].Text;
        dt.Rows[dt.Rows.Count - 1]["total"] = gvRow.Cells[5].Text;
        dt.AcceptChanges();
    }
    return dt;
    }

    private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
    {
    DataRow[] dr = dt.Select("id = '" + gvRow.Cells[1].Text + "'");
    if (dr.Length > 0)
    {
        dt.Rows.Remove(dr[0]);
        dt.AcceptChanges();
    }
    return dt;
    }

    protected void CheckBox_CheckChanged(object sender, EventArgs e)
    {
    GetData();
    SetData();
    BindSecondaryGrid();
    }

    private void BindSecondaryGrid()
    {
    DataTable dt = (DataTable)ViewState["SelectedRecords1"];
    gridview3.DataSource = dt;
    gridview3.DataBind(); 
    }
  }

,源代码是

<asp:GridView ID="gridview2" runat="server" AutoGenerateColumns="False" 
 DataKeyNames="id" DataSourceID="SqlDataSource5">
 <Columns>
  <asp:TemplateField>
   <HeaderTemplate>
    <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);"
                  AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged"/>
   </HeaderTemplate>
   <ItemTemplate>
       <asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"
               AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged" />
   </ItemTemplate>
  </asp:TemplateField>

  <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
           ReadOnly="True" SortExpression="id" />

  <asp:BoundField DataField="name" HeaderText="name" 
           SortExpression="name" />

  <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />

  <asp:BoundField DataField="quantity" HeaderText="quantity" 
           SortExpression="quantity" />

  <asp:BoundField DataField="total" HeaderText="total" SortExpression="total" />
 </Columns>
 </asp:GridView>

 <asp:SqlDataSource ID="SqlDataSource5" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Land]"></asp:SqlDataSource>
  <br />
  </div>
<div>

<asp:GridView ID="gridview3" runat="server"
  AutoGenerateColumns = "False" DataKeyNames="id" 
  EmptyDataText = "No Records Selected" >
<Columns>
  <asp:BoundField DataField = "id" HeaderText = "id" />
  <asp:BoundField DataField = "name" HeaderText = "name" ReadOnly="True" />
  <asp:BoundField DataField = "price" HeaderText = "price" 
         DataFormatString="{0:c}" ReadOnly="True" />
  <asp:TemplateField HeaderText="quantity">
  <EditItemTemplate>
     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("quantity")%>'</asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
     <asp:Label ID="Label1" runat="server" Text='<%# Bind("quantity") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:BoundField DataField = "total" HeaderText = "total" 
            DataFormatString="{0:c}" ReadOnly="True" />
            <asp:CommandField ShowEditButton="True" />

 </Columns>
</asp:GridView>

<asp:Label ID="totalLabel" runat="server"></asp:Label>
<br />

     </div>
    </form>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

这不是特定的asp.net解决方案,但这就是我在Windows应用程序中执行此类操作的方式。

首先,你应该确保GridView中有一个选定的行(gridView.SelectedRow != null)。 DataTable对象允许您通过不是通过数字索引而是通过DataRow - 类型对象索引访问它来访问所需行。在获得对要修改的字段值的行的引用后,请继续进行更改。

以下是示例:

if (gridView.SelectedRow != null)
{
    dataTable.Rows[gridView.SelectedRow].BeginEdit();
    dataTable.Rows[gridView.SelectedRow]["yourFieldName"] = newValue;
    dataTable.Rows[gridView.SelectedRow].EndEdit();
    gridView.DataSource = dataTable;
}

希望我的答案有任何帮助,因为我以前从未处理过asp.net。