0
private void button1_Click(object sender, RoutedEventArgs e)
{
  DataTable dt = new DataTable();
  dt.Columns.Add("id");
  dt.Columns.Add("Name");

  dt.Rows.Add("1","John");
  dataGrid1.ItemsSource = dt.DefaultView;
}

how i can add new row, on click my button? thanks :)

0

2 Answers 2

3

Sample Code :

Dynamically create table, add cloumn, add rows

1- Create a new DataTable

DataTable dt = new DataTable ("Table_AX"); 

2- Add columns to the DataTable

// Method 1 
dt.Columns.Add ("column0", System.Type.GetType ("System.String")); 
// Method 2 
DataColumn dc = new DataColumn("column1",System.Type.GetType("System.Boolean")); 
dt.Columns.Add (dc);

3- To add rows to the DataTable

// Initialize the row 
DataRow dr = dt.NewRow (); 
dr ["column0"] = "AX"; 
dr ["column1"] = true; 
dt.Rows.Add (dr); 
// Doesn't initialize the row 
DataRow dr1 = dt.NewRow (); 
dt.Rows.Add (dr1); 

private void button1_Click(object sender, RoutedEventArgs e)
{
  DataTable dt = new DataTable();
  dt.Columns.Add("id",System.Type.GetType ("System.String"));
  dt.Columns.Add("Name",System.Type.GetType ("System.String"));
  DataRow dr=dt.NewROw();
  dr[0]="a";
  dr[1]="abc";
  dt.Rows.Add(dr);
  dataGrid1.ItemsSource = dt.DefaultView;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this code

private void button1_Click(object sender, RoutedEventArgs e)
{

    DataTable dt = new DataTable();
     dt.Columns.Add("id");
     dt.Columns.Add("Name");
     DataRow dr = dt.NewRow();

     dr["id"]="testid";
     dr["Name"] = "testname";

     dt.Rows.Add(dr);
     dataGrid1.ItemsSource = dt.DefaultView;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.