I previously used datagridview but now changed it to use a datatable `
private void table()
{
//create a data set
DataSet ds = new DataSet();
//create a data table for the data set
DataTable dt = new DataTable();
//create some columns for the datatable
DataColumn dc = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Entry");
DataColumn dc3 = new DataColumn("SL");
DataColumn dc4 = new DataColumn("SL%");
DataColumn dc5 = new DataColumn("TP");
DataColumn dc6 = new DataColumn("TP%");
DataColumn dc7 = new DataColumn("Position");
DataColumn dc8 = new DataColumn("Day");
DataColumn dc9 = new DataColumn("Notes");
//add the columns to the datatable
dt.Columns.Add(dc);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
dt.Columns.Add(dc8);
dt.Columns.Add(dc9);
//add the datatable to the datasource
ds.Tables.Add(dt);
//make this data the datasource of our gridview
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoSize = true;
}
`
When using datagridview i used this code on a button click event to add a row
dataGridView1.Rows.Add(name, EntryPrice.Text, StopPrice.Text, slper, ProfitPrice.Text, tpper, pos, day, NotesTB.Text);
How do I add a row to the datatable with the same values using a button click event?
Using
dt.Rows.Add
isnt recognising the dt within my button click
CodePudding user response:
Found an answer from another user
var devents = (DataTable)dataGridView3.DataSource;
DataRow newRow = devents.NewRow();
newRow["start_time"] = new_starttime;
newRow["stop_time"] = new_stoptime;
newRow["cycle_time"] = new_cycletime;
devents.Rows.Add(newRow);
Credit to @kekusemau
CodePudding user response:
Hope this link helps you:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim table As DataTable = EEDataDataSet.Tables("Users")
' Use the NewRow method to create a DataRow
'with the table's schema.
Dim newuserrow As DataRow = table.NewRow()
' Set values in the columns:
newuserrow("Username") = Me.UsernameTextBox.Text
newuserrow("Password") = Me.PasswordTextBox.Text
' Add the row to the rows collection.
table.Rows.Add(newuserrow)
yourTableAdapter.updateAll(EEDataDataSet)
MsgBox("New User addition successful.")
Me.Close()
End Sub