2012-06-25 59 views
0

我想添加到我的網站一個簡單的表格/ excel-like feauture,將顯示一些值。所以,我添加了一個gridview。我根本不使用數據集或數據庫。我所有的是一個有兩個條目的小表格。當用戶點擊一個按鈕時,我想添加一些值到datagridview控件作爲一個新行。如何在VB.NET中向datagridview控件添加新記錄?如何在Visual Basic中將新記錄添加到datagridview控件中?

我曾嘗試這樣的代碼:

GridView1.Rows.Add(New String() {Value1, Value2}) 

,但我得到一個錯誤:

'添加' 是不是 'System.Web.UI.WebControls.GridViewRowCollection'

成員

我的ASPX:

<asp:GridView ID="GridView1" runat="server" Width="731px"> 
    <Columns> 
     <asp:BoundField HeaderText="Name" /> 
     <asp:BoundField HeaderText="Grade" /> 
    </Columns> 
</asp:GridView> 
+0

可以請你提供你的aspx標記和代碼 – fenix2222

+0

回答

0

好的。使用數據表像

protected void Page_Load(object sender,EventArgs e) 
{ 
    if(!IsPostback) 
    { 
     DataTable myDataTable = new DataTable(); 

     DataColumn myDataColumn; 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Value1"; 
     myDataTable.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Value2"; 
     myDataTable.Columns.Add(myDataColumn); 
     ViewState["Data"]=myDataTable; 
    } 
} 


protected void btnAdd(object sender,EventArgs e) 
{ 
    Datatable Dt=(Datatable)ViewState["Data"]; 
    DataRow Dr=Dt.NewRow(); 
    Dr[0]=TextBox1.Text; 
    Dr[1]=TextBox2.Text; 
    Dt.Rows.Add(Dr); 
    ViewState["Data"]=Dt; 
} 

希望這會對你有幫助。

0

這很簡單。你所要做的就是把你的網格綁定到任何一個Colleciton上,比如List,Array,DataTable。創建記錄類

Public Class MyRecord 
    Private _name As String = "" 
    Private _grade As Integer = "" 

    Public Property Name() As String 
     Get 
      Return Me._name  
     End Get  
     Set(ByVal value As String) 
      Me._name = Value 
     End Set 
    End Property 

    Public Property Grade() As Integer 
     Get 
      Return Me._grade 
     End Get  
     Set(ByVal value As Integer) 
      Me._grade = Value 
     End Set 
    End Property 

End Class 

在你的Page_Load綁定網格列出

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not IsPotback Then 

     Dim list As New List(Of MyRecord)() 
     list.Add(New MyRecord() With { _ 
         Key .Name = "dsfsdf", _ 
         Key .Grade = 45 _ 
     }) 
     list.Add(New MyRecord() With { _ 
         Key .Name = "dsfsd234f", _ 
         Key .Grade = 50 _ 
     }) 

     Session("MyList") = list 
     GridView1.DataSource = list 
     GridView1.DataBind() 
    End If 
End Sub 

然後用文本框和按鈕添加某種形式和處理按鈕on_click事件

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click 
    Dim list As New List(Of MyRecord)   

    If Session("MyList") Is Not Nothing Then 
     list = DirectCast(Session("MyList"), List(Of MyRecord)) 
    End If 

    list.Add(New MyRecord() With { _ 
         Key .Name = txt1.Text, _ 
         Key .Grade = Convert.ToInt32(txt2.Text) _ 
    }) 


    GridView1.DataSource = list 
    GridView1.DataBind() 

End Sub 

很抱歉,如果有任何錯誤,我是一個C#開發人員

相關問題