I am trying to add data to a gridview, for example if we select a value from a dropdown, enter a value in a textbox and then click on a button then that data is displayed in a gridview.
I successfully did this but my problem is when I then enter some other text and select other value then the previous record is hidden and a new record is displayed. I want to display both the records which I add.
Should I do this in a loop or something ?
Protected Sub pAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pAdd.Click
Dim DT= New DataTable()
Dim DR As DataRow = dtbl.NewRow()
DT.Columns.Add(New DataColumn("ID"))
DT.Columns.Add(New DataColumn("Name"))
DT.Columns.Add(New DataColumn("Type"))
DR (0) = TEXT_1.Text
DR (1) = ddl_name.SelectedItem.Text
DR (2) = ddl_type.SelectedItem.Text
DT.Rows.Add(drw1)
gridv.DataSource = DT
gridv.DataBind()
End Sub
答案 0 :(得分:0)
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Session["dt"] == null)
{
dt = new DataTable();
dt.Columns.Add("FirstColumn");
dt.Columns.Add("SecondColumn");
}
else
{
dt = (DataTable)Session["dt"];
}
DataRow row = dt.NewRow();
row["FirstColumn"] = text_User.Text;
row["SecondColumn"] = ddl_Use.SelectedIndex;
dt.Rows.Add(row);
Session["dt"] = dt;
gv_grid.DataSource = dt;
gv_grid.DataBind();
}
答案 1 :(得分:0)
我已经完成了使用这个
Private Sub AddToGrid()
If ViewState("table") IsNot Nothing Then
Dim dtable As DataTable = DirectCast(ViewState("table"), DataTable)
Dim drow As DataRow = Nothing
If dtable.Rows.Count > 0 Then
For i As Integer = 1 To dtable.Rows.Count
drow = dtable.NewRow()
drow("ID") = TEXT_1.Text
drow("Name") = ddl_name.SelectedItem.Text
drow("Type") = ddl_type.SelectedItem.Text
Next
dtable.Rows.Add( drow)
ViewState("table") =dtable
gridv.DataSource = dtable
gridv.DataBind()
End If
End If
End Sub