最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql server - Data doesn't show up in the datagridview in C#, only columns show up - Stack Overflow

programmeradmin1浏览0评论

I am trying to display data from a SQL Server database in a C# datagridview, but only the column headers are displayed and not the data itself. I checked the SQL Server database that I am connecting to by using a "EXEC" command and can verify that its working so I think it may have something to do with my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RonnaForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class columnTitle
        {
            public int Id { get; set; }
            public string Site { get; set; }
            public string ProductCode { get; set; }
            public string ProductName { get; set; }
            public string PrimaryRejectionReason { get; set; }
            public string SecondaryRejectionReason { get; set; }
            public string AdditionalComments { get; set; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable Dtable = new DataTable(); //This will create a new data table called Dtable
    
            try
            {
                // This is connecting to the SQL Server database
                using (var con = new SqlConnection("Data Source=ffgsqltest2;Initial Catalog=Test_Playground;Integrated Security=True;")) //this creates a connection to test playground

                // This retrieves the stored procedure that I want      
                using (var cmd = new SqlCommand("TechnicalRejects", con)) 
                // This should insert the columns
                using (var da = new SqlDataAdapter(cmd)) 
                {
                    cmd.Parameters.AddWithValue("@Site", CBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("@PrimaryRejectionReason", CBox2.Text.ToString());

                    cmd.CommandType = CommandType.StoredProcedure;

                    // this fills the table with the columns#
                    da.Fill(Dtable); 
                }

                DataGridView.DataSource = Dtable;
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occurred " + ex.Message);
            }

            // This adds the options for the first combo box
            CBox1.Text = "All"; 
            CBox1.Items.Add("Town1");
            CBox1.Items.Add("Town2");
            CBox1.Items.Add("Town3");
            CBox1.Items.Add("Town4");
            CBox1.Items.Add("Town5");

            // This makes the default selected "All"
            CBox1.SelectedIndex = 0; 

            // Add options for the second combo box
            CBox2.Text = "All"; 
            CBox2.Items.Add("All");
            CBox2.Items.Add("Label Issue");
            CBox2.Items.Add("Out of Specification");
            CBox2.Items.Add("Contamination");
            CBox2.Items.Add("Damaged");
            CBox2.Items.Add("Order Administration");
            CBox2.Items.Add("Other");

            // This makes the default selected "All"
            CBox2.SelectedIndex = 0; 
        }

        private void CBoxRejection_SelectedIndexChanged(object sender, EventArgs e)
        { 
            // THIS IS Cbox 1, This is for the Sites
            DataTable Otable = new DataTable();
    
            try
            {
                SqlConnection con = new SqlConnection("Data Source=ffgsqltest2;Initial Catalog=Test_Playground;Integrated Security=True;");
                SqlCommand cmd = new SqlCommand("TechnicalRejects", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Site", CBox1.Text.ToString());

                SqlDataAdapter dga = new SqlDataAdapter();
                dga.SelectCommand = cmd;
                dga.Fill(Otable);

                DataGridView.DataSource = Otable;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error has occurred when searching for sites " + ex.Message);
            }
        }

        private void CBox2_SelectedIndexChanged(object sender, EventArgs e)
        { 
            // This is for the rejection reasons - cbox2
            DataTable Otable = new DataTable();

            SqlConnection con = new SqlConnection("Data Source=ffgsqltest2;Initial Catalog=Test_Playground;Integrated Security=True;");

            SqlCommand cmd = new SqlCommand("TechnicalRejects", con);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Site", CBox1.Text.ToString());
            cmd.Parameters.AddWithValue("@PrimaryRejectionReason", CBox2.Text.ToString());

            SqlDataAdapter dga = new SqlDataAdapter();
            dga.SelectCommand = cmd;
            dga.Fill(Otable);

            DataGridView.DataSource = Otable;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            // Create a new DataTable to store the results
            DataTable Dtable = new DataTable(); 

            try
            {
                // Where the data is and tell it is a stored procedure
                SqlConnection con = new SqlConnection("Data Source=ffgsqltest2;Initial Catalog=Test_Playground;Integrated Security=True;");
                SqlCommand cmd = new SqlCommand("TechnicalRejects", con);
                cmd.CommandType = CommandType.StoredProcedure;

                // Add parameters for search filtering
                cmd.Parameters.AddWithValue("@Site", CBox1.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@PrimaryRejectionReason", CBox2.SelectedItem.ToString());

                SqlDataAdapter dga = new SqlDataAdapter(cmd);
                dga.Fill(Dtable); // Fill the DataTable with the search results

                // Bind the DataTable to the DataGridView
                DataGridView.DataSource = Dtable;
            }
            catch (Exception ex)
            {
                // Handle any errors
                MessageBox.Show("An error occurred while searching: " + ex.Message);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论