2009-08-08 57 views
1

所以我尝试了一个我的概念工具,我需要能够真正轻松地从数据库中读取和写入数据。我按照自己喜欢的方式设置了表单,并围绕不同的文本框和下拉框读取数据库中的数据。我已经掌握了所有工作和所有工作,但是有一个小错误,我不完全明白为什么在那里。某些文本框不会更新数据库中的文本。但它似乎只是在数据库中的数据不存在的情况下才会发生。因此,最后一行的值仍然悬挂在文本框中,因此单击“更新”实际上会将来自最后一行的字段的值更新为新行。搞砸了一切。从数据库读取/写入时设计代码流的最佳方式

现在,我最感兴趣的是代码的剪切流。布置代码来完成所有这些工作的最佳方式是什么?到目前为止,我有这样的:

这是代码在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 

的“选择” - 变量都在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

重新您的评论 - 我不知道一个可怕的很多关于适配器,说实话(我用ORM,而不是DataSet)。也许问作为另一个具体问题? – 2009-08-08 12:55:02

回答

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"); } } 
} 
+0

最好的提示呢!正如你可能会说,我不是真正的程序员。所以这个任务真的是相关的 - 如何真正绑定数据:) – 2009-08-08 08:54:52

+0

只是试图将文本框的值绑定到数据库中的列。它工作的很好,没有更多的bug :) 但是,数据不存储。试图插入以下内容: DataSourceDataSet.Songs.AcceptChanges() SongsTableAdapter.Update(Me。DataSourceDataSetSongs) 但数据库中的数据未更新 – 2009-08-08 10:29:11

0

尝试评论在错误恢复下一小会看到会发生什么。我设法混淆了我自己的次数,而不是我可以指望的那样。

编辑

我刚刚意识到这是VB.Net。在这种情况下,您不应该使用On Error Resume Next。使用Try Catch结构。

+0

好吧,我要试着去看看会发生什么。 任何有关如何让代码继续前进的想法,如果来自单元格的值是什么都没有? – 2009-08-08 08:51:38

+0

发现,这工作很好,thanx! :) – 2009-08-08 10:10:03

相关问题