Данные не отображаются в сетке View Data на C#, отображаются только столбцы.C#

Место общения программистов C#
Anonymous
Данные не отображаются в сетке View Data на C#, отображаются только столбцы.

Сообщение Anonymous »

Я пытаюсь отобразить данные из базы данных SQL на сетку C# View, но отображаются только заголовки столбцов, а не сами данные. Я проверил базу данных SQL, к которой я подключаюсь, используя команду «EXEC» и могу убедиться, что она работает, поэтому я думаю, что это может иметь какое -то отношение к коду < /p>
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 database
using (var con = new SqlConnection("Data Source=ffgsqltest2;Initial Catalog=Test_Playground;Integrated Security=True;")) //this creates a connection to test playground
using (var cmd = new SqlCommand("TechnicalRejects", con)) //This retrieves the stored procedure that I want

using (var da = new SqlDataAdapter(cmd)) //This should insert the columns
{
cmd.Parameters.AddWithValue("@Site", CBox1.Text.ToString());
cmd.Parameters.AddWithValue("@PrimaryRejectionReason", CBox2.Text.ToString());

cmd.CommandType = CommandType.StoredProcedure;
da.Fill(Dtable); //this fills the table with the columns#
}
DataGridView.DataSource = Dtable;
}
catch (Exception ex)
{
MessageBox.Show("An error has occured " + ex.Message);
}

CBox1.Text = "All"; //This adds the options for the first combo box
CBox1.Items.Add("Town1");
CBox1.Items.Add("Town2");
CBox1.Items.Add("Town3");
CBox1.Items.Add("Town4");
CBox1.Items.Add("Town5");
CBox1.SelectedIndex = 0; //This makes the default selected "All"
CBox2.Text = "All"; //Add options for the second combo box
CBox2.Items.Add("All");
CBox2.Items.Add("Label Issue");
CBox2.Items.Add("Out of Specification");
CBox2.Items.Add("Contaimination");
CBox2.Items.Add("Damaged");
CBox2.Items.Add("Order Administration");
CBox2.Items.Add("Other");
CBox2.SelectedIndex = 0; //This makes the default selected "All"
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}

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 occured 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)
{
DataTable Dtable = new DataTable(); // Create a new DataTable to store the results

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();
}
}
}


Подробнее здесь: https://stackoverflow.com/questions/795 ... ns-show-up

Вернуться в «C#»