Home > Software engineering >  How to print a particular database with its datagridview in C#
How to print a particular database with its datagridview in C#

Time:12-19

So I have multiple windows application forms. Every form has its own database displayed a datagridview. What I want to do is in a new form user should able select particular form's database from a dropdown menu and able view that database with its own datagridview and print them out. Please help. Very urgent for my project.

I dont know to load a particular database with its datagridview when a user click particular in a dropdown menu.

CodePudding user response:

To load a particular database with its DataGridView when a user selects it from a dropdown menu, you can follow these steps:

First, create a dropdown menu in your new form and populate it with the names of the forms whose databases you want to display.

In the event handler for the dropdown menu, you can retrieve the selected form name and use it to determine which database to load.

To load the database, you can use a data adapter to retrieve the data from the database and fill a DataTable with the results. You can then bind the DataTable to the DataGridView using the DataGridView's DataSource property.

To print the contents of the DataGridView, you can use the DataGridView's Print method. You may need to customize the print settings, such as the page layout and margins, using the PrintDocument and PrintDialog classes.

Here is some sample code that demonstrates how to do this:

private void Form1_Load(object sender, EventArgs e)
{
    // Populate the dropdown menu with the names of the forms
    FormList.Items.Add("Form1");
    FormList.Items.Add("Form2");
    FormList.Items.Add("Form3");
}

private void FormList_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the selected form name
    string formName = FormList.SelectedItem.ToString();

    // Load the database for the selected form
    string connectionString = ""; // Replace with the connection string for the selected form's database
    string sql = "SELECT * FROM table"; // Replace with the SQL query to retrieve the data for the selected form
    DataTable data = new DataTable();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(sql, connection))
        {
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(data);
        }
    }

    // Bind the data to the DataGridView
    dataGridView1.DataSource = data;
}

private void PrintButton_Click(object sender, EventArgs e)
{
    // Set up the print dialog
    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = printDocument1;
    printDialog.UseEXDialog = true;

    // Display the print dialog and print the DataGridView if the user clicks OK
    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        printDocument1.Print();
    }
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    // Print the contents of the DataGridView
    bool more = dataGridView1.Print(e);
    if (more == true)
        e.HasMorePages = true;
}

To display data from a database in a DataGridView control in another form, you can follow these steps:

Create a new form in your project and add a DataGridView control to it. In the form where you want to display the data, create an instance of the form with the DataGridView control and show it using the Show method: Copy code var dataForm = new DataForm(); dataForm.Show(); In the form with the DataGridView control, load the data from the database and bind it to the DataGridView. Here's an example of how you can do this using Entity Framework:

using (var context = new FormsContext())
{
    var formSubmits = context.FormSubmits.ToList();
    dataGridView1.DataSource = formSubmits;
}

This will display the data from the FormSubmits table in the DataGridView control.

  • Related