question

NazHim-9882 avatar image
0 Votes"
NazHim-9882 asked DanielZhang-MSFT commented

already have DataTable in form1.then how to use this DataTable from form2 in C#

hi all

already have DataTable in form1.then how to use this DataTable from form2
i'am used form1 code is at below. in C#

 studentsDetBindingSource = new BindingSource();
 studentsDetTableAdapter = new SqlDataAdapter();
 //
 sqlCmd = new SqlCommand();
 sqlCon = new SqlConnection(SqlSerConCla.SqlSerConStr());
 sqlCmd.Connection = sqlCon;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.Parameters.AddWithValue("@stuNam", this.stuNamTextBox.Text);
 sqlCmd.CommandText = "SELECT * FROM studentsDet WHERE (stuNam = @stuNam)";
 studentsDetTableAdapter = new SqlDataAdapter(sqlCmd);
 //
 studentsDetDataSet = new DataSet();
 studentsDetDataSet.Tables.Add("studentsDet");
 studentsDetDataSet.Tables["studentsDet"].Rows.Clear();
 DataTable studentsdet = studentsDataSet.Tables["studentsDet"];
 students.TableName = "studentsdet";
 //
 studentsDetTableAdapter.Fill(studentsdet);
 studentsDetBindingSource.DataSource = studentsdet;
 studentsDetBindingNavigator.BindingSource = studentsDetBindingSource;
 studentsDetDataGridView.DataSource = studentsDetBindingSource;
 //
 this.studentsDetBindingSource.Sort = "stuNam ASC";

in dataset table, bindingsource and SQL Table too. how can do insert, delete and updates?. used form1's datatable. from form2
can possible it?.
if possible. can give me provide some code?

with best regards
NazHim


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

One other thing, you can send the dataset with the datatable in it over to the other form via the form constructor too.

0 Votes 0 ·

hello DuaneArnold-0443

thanks for your fast response

i'am not get yet any idea. till now

best regards
NazHim

0 Votes 0 ·

Hi @NazHim-9882,
As DuaneArnold-0443 said, you can use the form constructor to pass the data table into the form.
Have you tried his code? And what specific problem did you encounter?
Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 edited

A datatable is an object everything in .NET is an object even string data is an object, as an example.

When you call the form, pass the datatable into the form using its form constructor.

https://blogs.msmvps.com/deborahk/passing-data-between-forms-constructor/


public partial class Form2: Form
{
// Variable to store the passed in text
datatable passedIndt;

 public Form2(datatable dt)
 {
     InitializeComponent();

     this.passedIndt= dt;
 }

}

private void Button1_Click(object sender, EventArgs e)
{
Form2 frmForm2 = new Form2(yourdatatable);
this.Hide();
frmForm2 .ShowDialog();
this.Show();
}

You pass it into the other form and have at it.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.