添加记录Nayf按钮后,如何在C#中更新DataGridView?
这是我的代码按钮在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
/ *尝试调用从数据库加载数据并在表中显示的功能,而不是刷新表 */
/* try calling the function that loads data from the database and displays in table instead of refreshing the table */