为啥我在 DataGridView 中有问号而不是文本?
Posted
技术标签:
【中文标题】为啥我在 DataGridView 中有问号而不是文本?【英文标题】:Why do i have question marks instead of text in DataGridView?为什么我在 DataGridView 中有问号而不是文本? 【发布时间】:2021-08-27 17:46:15 【问题描述】:所以我正在用 C# 和 Sql 编写一个程序,我在 Visual Studio 和 DataSet 中创建了一个数据库,并将它连接到 DataGridView。但问题是,当我在 DataGridView 行中插入一些内容时,所有字符串都显示为问号,但使用“int”类型一切正常。
namespace MH
public partial class PatientForm : Form
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Admin\source\repos\MH\MH\MH.mdf;Integrated Security=True");
public PatientForm()
InitializeComponent();
void populate()
conn.Open();
string query = "select * from Patient";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
PatientGV.DataSource = ds.Tables[0];
conn.Close();
private void label9_Click(object sender, EventArgs e)
Application.Exit();
private void PatientForm_Load(object sender, EventArgs e)
populate();
PatientGV.Columns[0].HeaderText = "ID пациента";
PatientGV.Columns[1].HeaderText = "Имя";
PatientGV.Columns[2].HeaderText = "Адрес";
PatientGV.Columns[3].HeaderText = "Телефон";
PatientGV.Columns[4].HeaderText = "Возраст";
PatientGV.Columns[5].HeaderText = "Пол";
PatientGV.Columns[6].HeaderText = "Группа крови";
PatientGV.Columns[7].HeaderText = "Основная болезнь";
private void button4_Click(object sender, EventArgs e)
Home h = new Home();
h.Show();
this.Hide();
private void button2_Click(object sender, EventArgs e)
conn.Open();
string query = "update Patient set PatName = '" + PatName.Text + "', PatAddress = '" + PatAd.Text + "', PatPhone = '" + PatPhone.Text + "', PatAge = '" + PatAge.Text + "', PatGender = '" + GenderCb.SelectedItem.ToString() + "', PatBlood = '" + BloodCb.SelectedItem.ToString() + "', PatDisease = '" + MajorTb.Text + "' where PatId = '" + PatId.Text + "'";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Пациент успешно обновлен");
conn.Close();
populate();
private void PatientGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
if (e.RowIndex >= 0)
DataGridViewRow row = this.PatientGV.Rows[e.RowIndex];
PatId.Text = row.Cells[0].Value.ToString();
PatName.Text = row.Cells[1].Value.ToString();
PatAd.Text = row.Cells[2].Value.ToString();
PatPhone.Text = row.Cells[3].Value.ToString();
PatAge.Text = row.Cells[4].Value.ToString();
MajorTb.Text = row.Cells[7].Value.ToString();
private void button1_Click(object sender, EventArgs e)
if (PatId.Text == "" || PatName.Text == "" || PatAd.Text == "" || PatPhone.Text == "" || PatAge.Text == "" || MajorTb.Text == "")
MessageBox.Show("Пустые поля не принимаются!");
else
conn.Open();
string query = "insert into Patient values(" + PatId.Text + ",'" + PatName.Text + "','" + PatAd.Text + "','" + PatPhone.Text + "'," + PatAge.Text + "," +
"'" + GenderCb.SelectedItem.ToString() + "','" + BloodCb.SelectedItem.ToString() + "','" + MajorTb.Text + "')";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Запись успешно добавлена!");
conn.Close();
populate();
private void button3_Click(object sender, EventArgs e)
if (PatId.Text == "")
MessageBox.Show("Введите ID пациента");
else
conn.Open();
string query = "delete from Patient where PatId=" + PatId.Text + "";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Пациент успешно удален");
conn.Close();
populate();
我很乐意为您提供任何帮助! 编辑:为澄清添加了表单代码
【问题讨论】:
你是对的,它是 nvarchar,并且是从组合框中选择的,所以我不确定是什么问题.. 问题是,当我直接插入数据库时,它看起来还不错,所以我猜问题出在 datagridview 的某个地方 我猜你在插入数据时将这些值视为varchar
,因此现在它们是代码页中没有的字符的?
字符。
所以我将代码编辑到原始帖子中,但我如何检查从数据库返回的数据?我的意思是,我检查了我从表单中插入的数据库中的数据,它是相同的问号,但是当我将数据插入数据库本身时,一切正常
"delete from Patient where PatId=" + PatId.Text + ""
- 请在继续之前以***.com/q/332365/11683 开头。这也将解决 your problem 作为副作用。然后请考虑删除表单级连接对象,并将所有Open
和Close
调用替换为using
。
【参考方案1】:
您可以尝试以下步骤来解决datagirdview中问号的问题。
首先,您可以在数据库中创建下表。
CREATE TABLE [dbo].[Patient] (
[Id] INT NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Age] INT NOT NULL,
[Address] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
其次,请尝试以下代码使用SqlParameters向数据库插入数据。
SqlConnection conn = new SqlConnection(@"CONNSTR");
void populate()
conn.Open();
string query = "select * from Patient";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
var ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
private void Form1_Load(object sender, EventArgs e)
populate();
private void button1_Click(object sender, EventArgs e)
if (txtId.Text == "" || txtName.Text == "" || txtAge.Text == "" || txtAddress.Text == "" )
MessageBox.Show("Пустые поля не принимаются!");
else
conn.Open();
string query = "insert into Patient(Id,Name,Age,Address)values(@Id,@Name,@Age,@Address)";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Id",Convert.ToInt32(txtId.Text));
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Age", Convert.ToInt32(txtAge.Text));
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Запись успешно добавлена!");
conn.Close();
populate();
结果:
【讨论】:
blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…以上是关于为啥我在 DataGridView 中有问号而不是文本?的主要内容,如果未能解决你的问题,请参考以下文章
我在C#里面给datagridview绑定了一个数据源,可当我判断值的时候却认为是空的,为啥呀?