[英]How do I sort a dataGridView the was populated with SqlDataReader and BindingSource , when i push a button
[英]How do I Update/Reload DataGridView BindingSource?
我是C#,Windows窗體和datagridviews的新手。 我有一個選項卡式表單:選項卡1顯示演習表的datagridview; 選項卡2用於向表格添加新練習。 練習表通過test_ExercisesDataSet,vwexercisesBindingSource,vw_ExercisesTableAdapter綁定到datagrid視圖。
我不確定重新切換到選項卡1時需要執行什么操作來重新綁定/刷新綁定源,以便刷新datagridview。如果我完全關閉該表單並重新啟動它,則可以在其中看到新行。桌子。
我已經在Web和StackOverflow上看到了很多示例,但是我仍然不明白我在做什么錯。
順便說一句,我正在使用Visual Studio 2010。
任何幫助表示贊賞!
謝謝!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testTabbedInterface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string GetConnectionString()
{
return connString;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'test_ExercisesDataSet.vw_exercises' table. You can move, or remove it, as needed.
this.vw_exercisesTableAdapter.Fill(this.test_ExercisesDataSet.vw_exercises);
exerciseListDataGridView.DataSource = this.test_ExercisesDataSet.vw_exercises;
}
private void InsertExercise(string exerciseName, string exerciseDescription, string exerciseBegin)
{
var conn = new SqlConnection(GetConnectionString());
const string InsertExerciseSql = @"INSERT INTO database.dbo.exercises
(PK_exerciseUID,
exerciseName,
exerciseDescription,
exerciseBegin,
exerciseEnd)
VALUES
(@PK_exerciseUID,
@exerciseName,
@exerciseDescription,
@exerciseBegin,
NULL)";
try
{
SqlCommand cmd = new SqlCommand(InsertExerciseSql, conn);
var param = new SqlParameter[4];
Guid exerciseGUID = Guid.NewGuid();
param[0] = new SqlParameter("@PK_exerciseUID", exerciseGUID);
param[1] = new SqlParameter("@exerciseName", exerciseName);
param[2] = new SqlParameter("@exerciseDescription", exerciseDescription);
//Convert date(s) to correct format
DateTime exerciseBeginConverted = Convert.ToDateTime(exerciseBegin);
param[3] = new SqlParameter("@exerciseBegin", exerciseBeginConverted);
foreach (SqlParameter t in param)
{
cmd.Parameters.Add(t);
}
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Test/Exercise, " + exerciseName + ", successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException ex)
{
string msg = "Error inserting into 'exercises': ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
private void saveExerciseButton_Click(object sender, EventArgs e)
{
InsertExercise(exerciseName.Text, exerciseDescription.Text, exerciseBegin.Text);
this.exerciseListDataGridView.EndEdit();
tabControl1.SelectTab("testExerciseTab");
}
private void addExButton_Click(object sender, EventArgs e)
{
tabControl1.SelectTab("exerciseTab");
}
private void reloadExListButton_Click(object sender, EventArgs e)
{
this.exerciseListDataGridView.Refresh();
}
}
}
創建一個LoadDataGridView方法:
private void LoadDataGridView() {
// Fill a DataAdapter using the SelectCommand.
DataAdapter da = null;
// The Sql code here
// In case something fails, bail out of the method.
if (da == null) return;
// Clear the DataSource or else you'll get double of everything.
if (exerciseListDataGridView.DataSource != null) {
exerciseListDataGridView.DataSource.Clear();
exerciseListDataGridView.DataSource = null;
}
// I'm doing this off the top of my head, you may need to fill a DataSet here.
exerciseListDataGridView.DataSource = da.DefaultView;
}
現在,您要做的就是在Form1_Load()和InsertExcercise()末尾調用該方法。 如果必須使用DataSet,請不要忘記在最后放置DataAdapter對象以節省資源。
插入后,您必須使用數據庫中的信息重新加載數據集。 這不是自動的!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.