添加记录Nayf按钮后,如何在C#中更新DataGridView?

发布于 2025-02-06 13:05:00 字数 1401 浏览 3 评论 0原文

这是我的代码按钮在datagridview数据上使用我的数据库中的SQL Server加载。单击按钮后,我想在DataGridView中更新数据。我该怎么做?

public void load_data_Click(object sender, EventArgs e)
{
        string text = "way_on_sql";
        SqlConnection conn = new SqlConnection(text);

        try
        {
            DataTable sotrud_table = new DataTable("Sotrud");  
            SqlDataAdapter load_table_sotrud = new SqlDataAdapter("SELECT * FROM sotrud", conn);

            load_table_sotrud.FillSchema(sotrud_table, SchemaType.Source);
            load_table_sotrud.Fill(sotrud_table);

            ds.Tables.Add(sotrud_table);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            if (ds.Tables.Count != 0)
            {
                grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
                grid_sotrud.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                grid_sotrud.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                grid_sotrud.MultiSelect = false;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

这是我的按钮更新代码 - 这无效:

private void update_data_Click(object sender, EventArgs e)
{
    grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
    grid_sotrud.Refresh();
}

It's my code button load on datagridview data with my database in SQL Server. After a click on the button, I want to update data in the datagridview. How can I do this?

public void load_data_Click(object sender, EventArgs e)
{
        string text = "way_on_sql";
        SqlConnection conn = new SqlConnection(text);

        try
        {
            DataTable sotrud_table = new DataTable("Sotrud");  
            SqlDataAdapter load_table_sotrud = new SqlDataAdapter("SELECT * FROM sotrud", conn);

            load_table_sotrud.FillSchema(sotrud_table, SchemaType.Source);
            load_table_sotrud.Fill(sotrud_table);

            ds.Tables.Add(sotrud_table);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            if (ds.Tables.Count != 0)
            {
                grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
                grid_sotrud.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                grid_sotrud.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                grid_sotrud.MultiSelect = false;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

This my button update code - and this does not work:

private void update_data_Click(object sender, EventArgs e)
{
    grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
    grid_sotrud.Refresh();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2025-02-13 13:05:00

/ *尝试调用从数据库加载数据并在表中显示的功能,而不是刷新表 */

private void update_data_Click(object sender, EventArgs e)
{
    grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
    load_data_Click();
}

/* try calling the function that loads data from the database and displays in table instead of refreshing the table */

private void update_data_Click(object sender, EventArgs e)
{
    grid_sotrud.DataSource = ds.Tables["Sotrud"].DefaultView;
    load_data_Click();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文