none
vb2008 DataGridView 中使用CalendarColumn()問題 即DataGridView中有此欄但DataTable無此欄該如何解? RRS feed

  • 問題

  • 親愛的各位大大:

    為了在DataGridView 中的某個欄位使用CalendarColumn()因此採用後加的方式添加欄位但在存檔時發現DataGridView中有此欄但DataTable無此欄該如何解? 其程式碼如下:

    Dim bbs As New SqlDataAdapter("SELECT PURTA.TA003, PURTA.TA001, PURTA.TA002, PURTB.TB003,CMSME.ME002,
                              CMSMV.MV047, PURTA.TA013, PURTA.TA016,PURTB.TB004, PURTB.TB005, PURTB.TB006,
                              PURTB.TB009,PURTB.TB007, PURTB.TB011, PURTB.TB032, PURTB.TB043,PURTB.TB039
                              FROM PURTA INNER JOIN PURTB ON PURTA.TA001 = PURTB.TB001 AND PURTA.TA002 = PURTB.TB002
                              INNER JOIN CMSMV ON PURTA.TA012 = CMSMV.MV001 INNER JOIN   CMSME ON PURTA.TA004 = CMSME.ME001
                              where PURTA.TA001=" + TextBox1.Text + " AND PURTA.TA002=" + TextBox2.Text + "", strDbCon1)
    bbs.Fill(ady, "aa")
    myDatatable1 = ady.Tables("aa")
    myDatatable1.Columns.Add("供應商")  //直接添加在DataTable中
    myDatatable1.Columns.Add("樣品類別")//直接添加在DataTable中
    myDatatable1.Columns.Add("採購人員")//直接添加在DataTable中
    DataGridView1.DataSource = myDatatable1
    Dim col As New CalendarColumn()
    col.HeaderText = "預計交貨日"
    DataGridView1.Columns.Add(col) //為了呼叫CalendarColumn() 所以添加在DataGridView1

    但在存檔時發現 這一欄有問題ProDate = New SqlBulkCopyColumnMapping("預計交貨日", "ProDate")  無此欄位

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyBulkcopy As SqlBulkCopy
            Dim TA003, TA001, TA002, TB003, ME002, MV047, TA013, TB004, TB005,
                TB006, TB009, TB007, TB011, TB032, TB043, TA016, TB039, MA002,
                ProDate, ProClass, Buyer As SqlBulkCopyColumnMapping
            Dim myDatatable3 As New DataTable
            myDatatable3 = DataGridView1.DataSource
            MyBulkcopy = New SqlBulkCopy(strDbCon)
            MyBulkcopy.DestinationTableName = "SampleQuery"
            TA003 = New SqlBulkCopyColumnMapping("TA003", "TA003")
            TA001 = New SqlBulkCopyColumnMapping("TA001", "TA001")
            TA002 = New SqlBulkCopyColumnMapping("TA002", "TA002")
            TB003 = New SqlBulkCopyColumnMapping("TB003", "TB003")
            ME002 = New SqlBulkCopyColumnMapping("ME002", "ME002")
            MV047 = New SqlBulkCopyColumnMapping("MV047", "MV047")
            TA013 = New SqlBulkCopyColumnMapping("TA013", "TA013")
            TB004 = New SqlBulkCopyColumnMapping("TB004", "TB004")
            TB005 = New SqlBulkCopyColumnMapping("TB005", "TB005")
            TB006 = New SqlBulkCopyColumnMapping("TB006", "TB006")
            TB009 = New SqlBulkCopyColumnMapping("TB009", "TB009")
            TB007 = New SqlBulkCopyColumnMapping("TB007", "TB007")
            TB011 = New SqlBulkCopyColumnMapping("TB011", "TB011")
            TB032 = New SqlBulkCopyColumnMapping("TB032", "TB032")
            TB043 = New SqlBulkCopyColumnMapping("TB043", "TB043")
            TA016 = New SqlBulkCopyColumnMapping("TA016", "TA016")
            TB039 = New SqlBulkCopyColumnMapping("TB039", "TB039")
            MA002 = New SqlBulkCopyColumnMapping("供應商", "MA002")
            ProDate = New SqlBulkCopyColumnMapping("預計交貨日", "ProDate")
            ProClass = New SqlBulkCopyColumnMapping("樣品類別", "ProClass")
            Buyer = New SqlBulkCopyColumnMapping("採購人員", "Buyer")
            MyBulkcopy.ColumnMappings.Add(TA003)
            MyBulkcopy.ColumnMappings.Add(TA001)
            MyBulkcopy.ColumnMappings.Add(TA002)
            MyBulkcopy.ColumnMappings.Add(TB003)
            MyBulkcopy.ColumnMappings.Add(ME002)
            MyBulkcopy.ColumnMappings.Add(MV047)
            MyBulkcopy.ColumnMappings.Add(TA013)
            MyBulkcopy.ColumnMappings.Add(TB004)
            MyBulkcopy.ColumnMappings.Add(TB005)
            MyBulkcopy.ColumnMappings.Add(TB006)
            MyBulkcopy.ColumnMappings.Add(TB009)
            MyBulkcopy.ColumnMappings.Add(TB007)
            MyBulkcopy.ColumnMappings.Add(TB011)
            MyBulkcopy.ColumnMappings.Add(TB032)
            MyBulkcopy.ColumnMappings.Add(TB043)
            MyBulkcopy.ColumnMappings.Add(TA016)
            MyBulkcopy.ColumnMappings.Add(TB039)
            MyBulkcopy.ColumnMappings.Add(MA002)
            MyBulkcopy.ColumnMappings.Add(ProDate)
            MyBulkcopy.ColumnMappings.Add(ProClass)
            MyBulkcopy.ColumnMappings.Add(Buyer)
            MyBulkcopy.WriteToServer(myDatatable3)
            MessageBox.Show("已存檔")

    問題:1.請問我該如何寫才能DataTable中直接加入CalendarColumn()使使用者能選取日期

    問題2.請問我該如何寫才能使Datagridview1所有見的到的欄位能夠移轉到DataTable上

            ps: Dim mytable2 as datatable

                  mytable2=datagridview1.Datasource

                 datagridview3.Datasource=mytable2

                仍然沒有發現預計交貨日這個欄位的資料

     所以請各位大大幫忙一下  感謝各位大大

    2010年7月20日 上午 06:11

解答

  • DataGridView有這一欄不代表它DataSource來源的DataTable也有這一欄

    所以你應該在DataTable中也要建立這一欄, 然後將該Column的DataPropertyName指向DataTable中的此欄位

    ex:

     Private myDatatable As New DataTable
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim col As New CalendarColumn()
            col.HeaderText = "預計交貨日"
            DataGridView1.Columns.Add(col)
            DataGridView1.Columns(0).DataPropertyName = "Col0"
            x()
        End Sub
        Private Sub x()
            myDatatable.Columns.Add("Col0", System.Type.GetType("System.DateTime"))
            Dim begin As Date = System.Convert.ToDateTime("2010/01/01")
            For i As Integer = 0 To 9
                Dim myRow As DataRow = myDatatable.NewRow
                myRow(0) = begin.AddDays(i)
                myDatatable.Rows.Add(myRow)
            Next
            DataGridView1.DataSource = myDatatable

        End Sub

    至於CalendarColumn的部份就不用貼了吧, 猜測你應該是參考 http://msdn.microsoft.com/zh-tw/library/7tas5c80%28VS.80%29.aspx


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。
    2010年7月20日 上午 06:38
    版主