 |
|
ขอสอบถามเกี่ยวกับ datagridview หน่อยครับ คือว่า มีคำสั่งไรบ้างครับ ที่จะให้จำนวนบรรทัดของ datagridvie |
|
 |
|
|
 |
 |
|
กรณีที่ DataSource ของ DataGridView ผูกกับ BindingSource
ก็อาจใช้วิธีนี้ได้
Code (VB.NET)
' dgv คือ DataGridView
Dim bs As BindingSource = dgv.DataSource
bs.AddNew
bs.Current("ชื่อฟิล์ด1") = "XXX"
bs.Current("ชื่อฟิล์ด2") = "XXX"
bs.Current("ชื่อฟิล์ด3") = "XXX"
...
bs.EndEdit()
|
 |
 |
 |
 |
Date :
2011-02-22 14:42:05 |
By :
หางอึ่ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดี๋ยวของเอาไปลองก่อนนะครับ
|
 |
 |
 |
 |
Date :
2011-02-22 15:00:28 |
By :
exclusiveM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ผมอยากได้มันคือ การพิมสดๆ ความต้องการแค่อยากให้บรรทัดว่างๆเพิ่มมาอีก 1 บรรทัดเมื่อเราเพิ่มข้อมูลเข้าไปบรรทัดสุดท้ายของกริด ปกติถ้าพิมพ์สดลงในกริด มันจะมีบรรทัดเพิ่มขึ้นมาโดยอัตโนมัติ แต่ถ้าส่งผ่านtextbox ลงกริด มันจะม่ะมีบรรทัดว่างเพิ่มมา มันต้องเขียนคำสั่งใส่ไป ประเด็นคือ ผมไม่รู้ว่าต้องเขียนคำสั่งว่าไง ช่วยแนะนำทีครับ
|
 |
 |
 |
 |
Date :
2011-02-23 08:59:05 |
By :
exclusiveM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ เหมือนเป็นการAddRow เปล่าๆไป 1 บรรทัดอ่ะครับ ถ้าเป็น vb6 โค้ดมันจะเป็นแบบนี้
Code (VB.NET)
with grid
.Rows = .Rows + 1
.Row = .Rows - 2
.RowHeight(.Row) = 330
.Col = 0: .Text = .Row
.Col = 1: .Text = ""
.Col = 1
end with
|
 |
 |
 |
 |
Date :
2011-02-23 09:04:48 |
By :
exclusiveM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เวลา Add มันก็มีเพิ่มมาให้อยู่แล้วนิครับ จขกท. เขียน Code ยังไงอ่ะครับถึงไม่มี ขอดูหน่อยได้ไหมครับ
|
 |
 |
 |
 |
Date :
2011-02-23 09:12:08 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือว่าาาา กริดปกติเเล้ว สามารถคลิกเเล้ว สามารถที่จะพิมลงไปในCell ได้เลย เมื่อทำการพิมพ์เข้าไป จะมีRow ใหม่เพิ่มขึ้นมาอีกRow ถูกม่ะครับ แต่ผมใช้วิธี เวลาเอาเม้าไปคลิกที่ๆผมกำหนดไว้ สมมุติ ถ้าเอาเม้าไปคลิกที่คอลั่มที่1 จะมีtextboxที่ Visible ไว้ ทำการ Resize ไปโชล ณ จุดCell ที่ทำการคลิกใน คอลัมที่1 ผมสั่งไว้ว่า ถ้าคลิกตรงนั้น ให้ Textbox ทำการResizeแล้วโชล แล้วทำการ Autonumber โชลทันที ที่คอลัมที่0 การที่ใช้วิธีนี้ ทำให้ไม่มีRowใหม่เพิ่มขึ้นมา คือผมอยากได้โค้ดสั่งให้มีRowใหม่โชลขึ้นมาต่อท้าย หลังจาก Enter เมื่อพิมพ์เสร็จ
ผมลองใช้วิธีนี้ดู แต่ มันไม่เพิ่มที่แถวสุดท้าย กลายเป็นมั่วไปหมด คือ
Code (VB.NET)
Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell
Dim i As Integer = 0
i = z
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = ""
dgvCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = ""
dgvRow.Cells.Add(dgvCell)
DataGridView1.Rows.Add(dgvRow)
|
 |
 |
 |
 |
Date :
2011-02-23 09:39:23 |
By :
exclusiveM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าผมจะเปลี่่ยนวิธีเขียน ผมจะทำการเพิ่มและลบข้อมูลลงในกริดเลย คือผมอยากจะถามว่า ผมจะทำการ Enabled = false ตามคอลัมที่กำหนด ได้หรือไม่ครับ สมมุติผมมีคอลัม 2 คอลัม คือ 0 และ 1 ผมอยากจะEnabled = false ที่คอลัม 0 ผมต้องเขียนคำสั่งว่าไรครับ ช่วยแนะนำที จุดประสงคือ ไม่อยากให้user ไปยุ่งกับ คอลัมที่0 เพราะจะให้ทำเป็น autonumber จะให้user ทำการเพิ่มและแก้ไขได้แค่ส่วนคอลัมที่ 1
อีกคำถามครับ คือเวลาจะเพิ่มหรือลบข้อมูลในกริด ปกติเเล้ว เราต้องคลิกดับเบิ้ลคลิกที่Cell ที่จะแก้ไข cusor มันก้อจะขึ้นเพื่อให้เราแก้ไขหรือเพิ่มข้อมูลลงกริดได้ แต่ถ้าผมอยากคลิกแค่ คลิกเดว cusor ก้อจะขึ้นให้พร้อมเพิ่มและแก้ไขในกริด ผมต้องไปเซตตรงไหนหรอครับ ช่วยแนะนำทีครับ ขอบคุณค๊าบ
|
 |
 |
 |
 |
Date :
2011-02-23 09:57:34 |
By :
exclusiveM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้แบบนี้ได้ไหมครับ ตอน Click อ่ะครับ
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, 1)
TextBox1.Focus()
ส่วน TextChanged ก็
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value = TextBox1.Text
มั่วๆ เอา
|
 |
 |
 |
 |
Date :
2011-02-23 13:01:44 |
By :
kaimuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เผื่อจะมีประโยชน์ครับ
Code (VB.NET)
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private buttonPanel As New Panel
Private WithEvents songsDataGridView As New DataGridView
Private WithEvents addNewRowButton As New Button
Private WithEvents deleteRowButton As New Button
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
SetupLayout()
SetupDataGridView()
PopulateDataGridView()
End Sub
Private Sub songsDataGridView_CellFormatting(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles songsDataGridView.CellFormatting
If e IsNot Nothing Then
If Me.songsDataGridView.Columns(e.ColumnIndex).Name = _
"Release Date" Then
If e.Value IsNot Nothing Then
Try
e.Value = DateTime.Parse(e.Value.ToString()) _
.ToLongDateString()
e.FormattingApplied = True
Catch ex As FormatException
Console.WriteLine("{0} is not a valid date.", e.Value.ToString())
End Try
End If
End If
End If
End Sub
Private Sub addNewRowButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles addNewRowButton.Click
Me.songsDataGridView.Rows.Add()
End Sub
Private Sub deleteRowButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles deleteRowButton.Click
If Me.songsDataGridView.SelectedRows.Count > 0 AndAlso _
Not Me.songsDataGridView.SelectedRows(0).Index = _
Me.songsDataGridView.Rows.Count - 1 Then
Me.songsDataGridView.Rows.RemoveAt( _
Me.songsDataGridView.SelectedRows(0).Index)
End If
End Sub
Private Sub SetupLayout()
Me.Size = New Size(600, 500)
With addNewRowButton
.Text = "Add Row"
.Location = New Point(10, 10)
End With
With deleteRowButton
.Text = "Delete Row"
.Location = New Point(100, 10)
End With
With buttonPanel
.Controls.Add(addNewRowButton)
.Controls.Add(deleteRowButton)
.Height = 50
.Dock = DockStyle.Bottom
End With
Me.Controls.Add(Me.buttonPanel)
End Sub
Private Sub SetupDataGridView()
Me.Controls.Add(songsDataGridView)
songsDataGridView.ColumnCount = 5
With songsDataGridView.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataGridView.Font, FontStyle.Bold)
End With
With songsDataGridView
.Name = "songsDataGridView"
.Location = New Point(8, 8)
.Size = New Size(500, 250)
.AutoSizeRowsMode = _
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.RowHeadersVisible = False
.Columns(0).Name = "Release Date"
.Columns(1).Name = "Track"
.Columns(2).Name = "Title"
.Columns(3).Name = "Artist"
.Columns(4).Name = "Album"
.Columns(4).DefaultCellStyle.Font = _
New Font(Me.songsDataGridView.DefaultCellStyle.Font, FontStyle.Italic)
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.Dock = DockStyle.Fill
End With
End Sub
Private Sub PopulateDataGridView()
Dim row0 As String() = {"11/22/1968", "29", "Revolution 9", _
"Beatles", "The Beatles [White Album]"}
Dim row1 As String() = {"1960", "6", "Fools Rush In", _
"Frank Sinatra", "Nice 'N' Easy"}
Dim row2 As String() = {"11/11/1971", "1", "One of These Days", _
"Pink Floyd", "Meddle"}
Dim row3 As String() = {"1988", "7", "Where Is My Mind?", _
"Pixies", "Surfer Rosa"}
Dim row4 As String() = {"5/1981", "9", "Can't Find My Mind", _
"Cramps", "Psychedelic Jungle"}
Dim row5 As String() = {"6/10/2003", "13", _
"Scatterbrain. (As Dead As Leaves.)", _
"Radiohead", "Hail to the Thief"}
Dim row6 As String() = {"6/30/1992", "3", "Dress", "P J Harvey", "Dry"}
With Me.songsDataGridView.Rows
.Add(row0)
.Add(row1)
.Add(row2)
.Add(row3)
.Add(row4)
.Add(row5)
.Add(row6)
End With
With Me.songsDataGridView
.Columns(0).DisplayIndex = 3
.Columns(1).DisplayIndex = 4
.Columns(2).DisplayIndex = 0
.Columns(3).DisplayIndex = 1
.Columns(4).DisplayIndex = 2
End With
End Sub
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
|
 |
 |
 |
 |
Date :
2011-08-11 17:18:06 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|