c# - 从 MySql db 到 c# 的重复列,为什么?

标签 c# mysql datagridview

id pojam opis id pojam opis(duplicate columns) 我只是测试从 db 到 c# 的连接,而不是列 id pojam opis 我实际上在 c# 的 datagridview 中获取 id pojam opis id pojam opis 。这是我用来将数据库连接到 C# 的部分代码:

public partial class Form1 : Form
{
    BindingSource bindingSource1 = new BindingSource();
    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=test;user=root;password=;");

    public Form1()
    {
        this.Load += new System.EventHandler(Form1_Load);
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {  
        dataGridView1.DataSource = bindingSource1;
        ubaciPodatke();        
    }
    public void ubaciPodatke()
    {
        try
        {
            MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM csharp", conn);
            DataTable table = new DataTable();
            da.Fill(table);
            bindingSource1.DataSource = table;    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);       
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是我的错误,还是它必须与phpmyadmin:版本信息:4.7.0-rc1有关。我在想这可能会发生,因为这个版本还不稳定?

编辑:如果我删除这个 form.resx file 你觉得怎么样? 用户添加了行,这可以解决问题吗?

最佳答案

几件事:
无需在 Load 事件中进行数据绑定(bind)。您可以在 InitializeComponent() 调用之后在构造函数中执行此操作。这与你的问题无关。

我不知道 ubaciPodatke 是什么意思,因为我不会说那种语言,但希望它相当于 InitializeGrid()。在这种情况下你应该把

dataGridView.DataSource = null;
dataGridView.Rows.Clear();
dataGridView.Columns.Clear(); 

作为尝试后的第一行。我会这样做“干净初始化”,因为我有时会在设计器中设计网格,出于“文档和可视化”的目的,但会从代码中执行实际的网格。此外,它还使 InitializeGrid() 保持可重用,以便从其他地方调用。

另外,与评论中的问题相关,

Am I reading data from dgv wrong?

绑定(bind)时,您应该从绑定(bind)的 DataTable 读取数据,而不是直接从 DataGridView 读取数据。

关于c# - 从 MySql db 到 c# 的重复列,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42748902/

相关文章:

mysql - 寻找平均最高工资

php - 昨天和今天在mysql中的日期

c# - 防止对 DataGridView 中的特定行进行编辑

mysql - 在 Datagridview 中以格式显示数据

c# - 高性能内存缓存的线程安全

c# - ASP.NET MVC, 'Ticket Required' 属性

C#泛型B+树

c# - Knockoutjs 绑定(bind)对象问题

php - array_unique 不工作?

c# - DataGridView 行高自动调整大小