单击按钮完全可编辑gridview点击

问题描述:

我有一个gridview和2个按钮外面。在按钮上点击gridview的所有行应该可以编辑。我正在使用ITemplate类完成此任务,但无法完成此任务。下面单击按钮完全可编辑gridview点击

是我的代码,以获得可编辑的GridView:

public class GridViewTemplate : ITemplate 
{ 
private DataControlRowType templateType; 
private string columnName; 
private string columnNameBinding; 
private string controlType; 

public GridViewTemplate(DataControlRowType type, string colname, string colNameBinding, string ctlType) 
{ 
    templateType = type; 
    columnName = colname; 
    columnNameBinding = colNameBinding; 
    controlType = ctlType; 
} 

public void InstantiateIn(System.Web.UI.Control container) 
{ 
    switch (templateType) 
    { 
     case DataControlRowType.Header: 
      Literal lc = new Literal(); 
      lc.Text = columnName; 
      container.Controls.Add(lc); 
      break; 
     case DataControlRowType.DataRow: 
      if (controlType == "Label") 
      { 
       Label lb = new Label(); 
       lb.ID = "lblName"; 
       lb.DataBinding += new EventHandler(this.OnDataBinding); 
       container.Controls.Add(lb); 
      } 
      else if (controlType == "TextBox") 
      { 
       TextBox tb = new TextBox(); 
       tb.ID = "txtWeightage" + columnNameBinding; 
       tb.DataBinding += new EventHandler(this.OnDataBinding); 
       container.Controls.Add(tb); 
      } 
      default: 
      break; 
    } 
} 
public void OnDataBinding(object sender, EventArgs e) 
{ 
    object bound_value_obj = null; 
    Control ctrl = (Control)sender; 
    IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer; 
    bound_value_obj = DataBinder.Eval(data_item_container.DataItem, columnName); 

    switch (templateType) 
    { 
     case DataControlRowType.Header: 
      Label lb = (Label)sender; 
      lb.Text = bound_value_obj.ToString(); 
      break; 
     case DataControlRowType.DataRow: 
      TextBox txtbox = (TextBox)sender; 
      txtbox.Text = bound_value_obj.ToString(); 
      break; 
    } 
} 
} 
+0

请参阅此[MSDN文章](http://msdn.microsoft.com/en-us/library/aa992036(v = vs.100).aspx)以获得帮助。 – Nunners

+1

你试过了什么? –

+0

任何人都可以帮我实现这个代码在我的按钮点击和其他相关的GridView事件... – reesha

它的一个样本:

这篇文章演示了如何使用C#asp.net做一个GridView编辑所有的时间。

step1-:与下面的列创建名为 “项” 表( “iterm_no”, “姓名”, “价格”) 其中 “iterm_no” 是身份column.as描述如下

create table items 
     (
      iterm_no int identity(1,1), 
      name varchar(20), 
      price numeric(10,2) 
     ) 

第2步 - 现在创建一个名称为(EditableGridView)的网页。如下所述。

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditableGridView.aspx.cs" Inherits="EditableGridView" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Editable GridView</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" DataKeyNames="INTERM_NO" > 
<FooterStyle BackColor="White" ForeColor="#000066" /> 
<Columns> 
<asp:BoundField DataField="INTERM_NO" HeaderText="ITEM_N0" InsertVisible="False" ReadOnly="True" 
SortExpression="INTERM_NO" /> 
<asp:TemplateField HeaderText="NAME" SortExpression="NAME"> 
<ItemTemplate> 
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NAME") %>' 
OnTextChanged="TextBox_TextChanged" BorderStyle="None"></asp:TextBox> 
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("INTERM_NO") %>' /> 
</ItemTemplate> 
</asp:TemplateField> 


<asp:TemplateField HeaderText="PRICE" SortExpression="PRICE"> 
<ItemTemplate> 
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PRICE") %>' 
OnTextChanged="TextBox_TextChanged" BorderStyle="None"></asp:TextBox> 
</ItemTemplate> 
</asp:TemplateField> 



</Columns> 
<RowStyle ForeColor="#000066" /> 
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
</asp:GridView> 
    </div> 
    <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" 
     Text="Update" /> 
    <asp:Label ID="lblMessage" runat="server"></asp:Label> 
    </form> 
</body> 
</html> 

步骤3-如下所述转到您的代码(EditableGridView.cs)。

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

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

    SqlConnection con; 
    bool[] rowChanged; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string conString = ConfigurationSettings.AppSettings["mycon"]; 
     con = new SqlConnection(conString); 
     int totalRows = GridView1.Rows.Count; 
     rowChanged = new bool[totalRows]; 
     if (!Page.IsPostBack) 
     { 
      BindGrid(); 
     } 
    } 

    public void BindGrid() 
    { 
     SqlDataAdapter adap = new SqlDataAdapter("select * from items", con); 
     DataTable dt = new DataTable(); 
     adap.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

    protected void TextBox_TextChanged(object sender, EventArgs e) 
    { 
     TextBox thisTextBox = (TextBox)sender; 
     GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent; 
     int row = thisGridViewRow.RowIndex; 
     rowChanged[row] = true; 
    } 



    protected void btnUpdate_Click(object sender, EventArgs e) 
    { 
     if (Page.IsPostBack) 
     { 
      int totalRows = GridView1.Rows.Count; 
      for (int r = 0; r < totalRows; r++) 
      { 
       if (rowChanged[r]) 
       { 
        GridViewRow thisGridViewRow = GridView1.Rows[r]; 
        HiddenField hf1 = (HiddenField)thisGridViewRow.FindControl("HiddenField1"); 
        string pk = hf1.Value; 
        TextBox tb1 = (TextBox)thisGridViewRow.FindControl("TextBox1"); 
        string name = tb1.Text; 
        TextBox tb2 = (TextBox)thisGridViewRow.FindControl("TextBox2"); 
        decimal price = Convert.ToDecimal(tb2.Text); 

        SqlCommand cmd = new SqlCommand("update items set name='" + name + "' , price='" + price + "' where INTERM_NO=' " + pk + "'", con); 
        if (con.State == ConnectionState.Closed) 
        { 
         con.Open(); 
        } 
        int temp = cmd.ExecuteNonQuery(); 
        if (temp > 0) 
        { 
         lblMessage.Text = "Operation perform successfully"; 
         con.Close(); 
        } 


       } 
      } 
      GridView1.DataBind(); 
      BindGrid(); 
     } 
    } 
} 

注:如果您对这篇文章有任何疑问或问题请联系我们:

或尝试this。此链接对您的要求有帮助

+0

感谢您的答案,但我的数据源不固定,所以我不能绑定列到绑定字段中的GridView ... – reesha

+0

我不是能够把我的代码在这里:( – reesha