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

NazHim 201 Reputation points
2021-06-11T06:00:03.643+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,852 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,221 Reputation points
    2021-06-11T15:12:16.893+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.