所以我正在尝试使用我的概念工具,我需要能够从数据库中读取和写入数据。我已经按照自己喜欢的方式设置了表单,并在不同的文本框和下拉框中展开,以便从数据库中读取数据。而且我已经完成了所有这些工作,但有一个小错误,我不完全理解为什么那里。某些文本框不会更新数据库中的文本。但似乎只有在数据库中的数据无效时才会发生。因此,最后一行的值仍然悬挂在文本框中,因此,单击“更新”实际上会将字段中的值从最后一行更新为新行。弄清楚一切。
现在,我最感兴趣的是代码的剪切流。制定代码来完成所有这些的最佳方法是什么?到目前为止,我有这个:
这是点击datagridview中的单元格时的代码:
Private Sub DataGridView_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick
On Error Resume Next
selectedName = Me.DataGridView.CurrentRow.Cells(0).Value
selectedGenre = Me.DataGridView.CurrentRow.Cells(1).Value
selectedRhytm = Me.DataGridView.CurrentRow.Cells(2).Value
selectedLength = Me.DataGridView.CurrentRow.Cells(3).Value
selectedFinished = Me.DataGridView.CurrentRow.Cells(4).Value
selectedSoundFile = Me.DataGridView.CurrentRow.Cells(5).Value
txtBoxName.Text = selectedName
txtBoxGenre.Text = selectedGenre
txtBoxRhytm.Text = selectedRhytm
txtBoxLength.Text = selectedLength
txtBoxFinished.Text = selectedFinished
txtBoxSoundFile.Text = selectedSoundFile
End Sub
“selected”变量都是在GlobalCode.vb中声明的。我已经创建了所有这些变量以供以后使用。它们的定义如下:
Friend Module GlobalVariables
Friend selectedName As String = Nothing
Friend selectedGenre As String = Nothing
Friend selectedRhytm As String = Nothing
Friend selectedLength As String = Nothing
Friend selectedFinished As String = Nothing
Friend selectedSoundFile As String = Nothing
End Module
我之前没有真正做过这样的事情。我更像是一名设计师而不是程序员,但我真的需要尝试一个概念,所以我不确定这是否是这样做的方式。我发现大多数时候它都有效。但我认为熟练的程序员有一种设计代码布局的方法,因此它高效,干净且易于阅读。 那看起来怎么样?
答案 0 :(得分:2)
(我无法在问题中看到任何与数据库相关的内容,顺便说一句)
也许布置此代码的最佳方式是......不是。不要为标准数据绑定框架可以处理的事情编写代码。例如(抱歉它是C#,但它应该翻译 - 这里的所有“好”位都是由.NET框架提供的,而不是语言);一些UI代码 - 请注意没有复制值的代码:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
// some sample data
BindingList<Track> tracks = new BindingList<Track>();
tracks.Add(new Track { Name = "foo", Genre = "Rock", Rhythm = "insane", Length = 180 });
tracks.Add(new Track { Name = "bar", Genre = "Classic", Rhythm = "sedate", Length = 240 });
// show the data on a form
using (Form form = new Form {
Controls = {
new DataGridView { DataSource = tracks, Dock = DockStyle.Fill },
new TextBox { DataBindings = {{"Text", tracks, "Name"}}, Dock = DockStyle.Bottom},
new TextBox { DataBindings = {{"Text", tracks, "Genre"}}, Dock = DockStyle.Bottom},
new TextBox { DataBindings = {{"Text", tracks, "Rhythm"}}, Dock = DockStyle.Bottom},
new TextBox { DataBindings = {{"Text", tracks, "Length"}}, Dock = DockStyle.Bottom},
}
}) {
Application.Run(form);
}
}
}
支持数据实体:
class Track : INotifyPropertyChanged {
private string name, genre, rhythm;
private int length;
public event PropertyChangedEventHandler PropertyChanged;
private void SetField<T>(ref T field, T value, string propertyName) {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public string Name { get { return name; } set { SetField(ref name, value, "Name"); } }
public string Genre { get { return genre; } set { SetField(ref genre, value, "Genre"); } }
public string Rhythm { get { return rhythm; } set { SetField(ref rhythm, value, "Rhythm"); } }
public int Length { get { return length; } set { SetField(ref length, value, "Length"); } }
}
答案 1 :(得分:0)
尝试注释On Error Resume Next,看看会发生什么。我设法让自己混淆的次数超过了我对这句话的重视程度。
修改
我刚才意识到这是VB.Net。在这种情况下,您不应该使用On Error Resume Next。使用Try Catch结构。