Dim sqlHuman As String
'Dim da As SqlDataAdapter
Dim ds As New DataSet
sqlHuman = "SELECT Production.ProductionID,GenerateFG_Details.SectionControl FROM GenerateFG_Details Inner Join Production ON (GenerateFG_Details.GenerateID = Production.GenerateID) Where (Production.ProductionID=@ProductionID) GROUP BY GenerateFG_Details.SectionControl,Production.ProductionID"
With com
.CommandText = sqlHuman
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@ProductionID", SqlDbType.NVarChar).Value = cboGenFG.SelectedValue.ToString()
dr = .ExecuteReader()
End With
If dr.HasRows Then
Dim dtSectionControl As DataTable
dtSectionControl = New DataTable()
dtSectionControl.Load(dr)
dgvSectionList.DataSource = dtSectionControl
FormatdgvStockList()
End If
With dgvSectionList
If .RowCount > 0 Then
Dim i As Integer = 0
For i = 0 To dgvSectionList.Rows.Count - 1
.Columns(0).HeaderText = "ลำดับ"
dgvSectionList.Rows(i).Cells(0).Value = i + 1
'.Columns(1).HeaderText = "เลขที่ผลิต"
.Columns(1).HeaderText = "หน่วย"
.Columns(0).Width = 40
.Columns(1).Width = 5
'.Columns(3).Width = 40
Next
End If
End With
Dim Table1 As Data.DataTable
Table1 = New DataTable("TEST")
Dim ds As New DataSet()
Dim Row As DataRow
Dim SEQ As Integer
Dim Col0 As DataColumn = New DataColumn("SEQ")
Col0.DataType = System.Type.GetType("System.String")
FOR LOOP
Row = Table1.NewRow()
SEQ += 1
Row.Item(0) = SEQ
Table1.Rows.Add(Row)
END FOR
ds = New DataSet()
ds.Tables.Add(Table1)
GridView1.DataSource = ds.Tables("TEST")
GridView1.DataBind()
Private Sub dg_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dg1.RowPostPaint
'สร้างเลขบรรทัด
Using b As SolidBrush = New SolidBrush(dg1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
sender.DefaultCellStyle.Font, _
b, _
e.RowBounds.Location.X + 40, _
e.RowBounds.Location.Y + 3, _
A_Right)
End Using
End Sub
ของส่งให้ใหม่ครับ เพิ่งรู้ว่าสามารถทำสีเหมือน VS TextEditor ได้
Code (VB.NET)
Private Sub dg_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dg1.RowPostPaint
'สร้างเลขบรรทัด
Using b As SolidBrush = New SolidBrush(dg1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
sender.DefaultCellStyle.Font, _
b, _
e.RowBounds.Location.X + 40, _
e.RowBounds.Location.Y + 3, _
A_Right)
End Using
End Sub
Private Sub dg_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dg1.RowPostPaint
'สร้างเลขบรรทัด
Using b As SolidBrush = New SolidBrush(dg1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
sender.DefaultCellStyle.Font, _ 'error ตรงDefaultCellStyle
b, _
e.RowBounds.Location.X + 40, _
e.RowBounds.Location.Y + 3, _
A_Right) 'error ตรงA_Right
End Using
End Sub
Dim cmdBuilder As System.Text.StringBuilder = New cmdBuilder As System.Text.StringBuilder()
cmdBuilder.AppendLine("SELECT ROW_NUMBER() ")
cmdBuilder.AppendLine(" OVER (ORDER BY GenerateFG_Details.SectionControl,Production.ProductionID) AS RowNo")
cmdBuilder.AppendLine(" ,Production.ProductionID,GenerateFG_Details.SectionControl")
cmdBuilder.AppendLine("FROM GenerateFG_Details")
cmdBuilder.AppendLine(" Inner Join Production")
cmdBuilder.AppendLine(" ON (GenerateFG_Details.GenerateID = Production.GenerateID)")
cmdBuilder.AppendLine("Where (Production.ProductionID=@ProductionID)")
cmdBuilder.AppendLine("GROUP BY GenerateFG_Details.SectionControl,Production.ProductionID")
With com
.CommandText = cmdBuilder.ToString()
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@ProductionID", SqlDbType.NVarChar).Value = cboGenFG.SelectedValue.ToString()
dr = .ExecuteReader()
End With
จะมี Column ชื่อ RowNo ที่เป็นหมายเลขแถวตามที่ต้องการและถ้าคุณลบข้อมูลออก ก็ให้ refresh datasource ใหม่
หมายเลขจะยังคงรันอัตโนมัติเสมอและใช้ได้ทั้ง winapp - webapp ในกรณีของ ms sql server 2005 ขึ้นมาค่ะ
ส่วนผมทำแบบนี้ก็ได้เหมือนกัน ไม่รู้จะเป็นไรไหม
With dgvGenerateFG_DetailsList
Dim i As Integer
Dim Column1 As New DataGridViewTextBoxColumn
Me.dgvGenerateFG_DetailsList.Columns.Insert(0, Column1)
For i = 1 To dgvGenerateFG_DetailsList.RowCount - 1
dgvGenerateFG_DetailsList.Rows(i - 1).Cells(0).Value = i
Next
If .RowCount > 0 Then
.Columns(0).HeaderText = "ลำดับ"
end if
end with
ขอโทษครับ ไม่ทันดูตัวแปร A_Right คือ Format การพิมพ์ชิดขวา DrawString เพื่อให้ตำแหน่งสุดท้ายตรงกัน
ส่วน sender.DefaultCellStyle.Font = Font ของตาราง dg1 สามารถใช้ฟอนต์สร้างใหม่ หรือฟอนต์ของฟอร์มก็ได้
ผมของแก้ไขให้เป็นดังนี้
Code (VB.NET)
Private Sub dg_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) _
Handles dg1.RowPostPaint, dg2.RowPostPaint
'สร้างเลขบรรทัด
Using b As SolidBrush = New SolidBrush(dg1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
sender.DefaultCellStyle.Font, _
b, _
e.RowBounds.Location.X + 40, _
e.RowBounds.Location.Y + 3, _
New StringFormat(StringFormatFlags.DirectionRightToLeft))
End Using
End Sub
'สร้าง DataGridView ใช้ชื่อว่า "dg1"
Public Class F14ShowRowIndex
Private dt As New DataTable
Private Sub Form32_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("ItmeName", GetType(System.String))
For i As Integer = 0 To 10
Dim dr As DataRow = dt.NewRow
dr(0) = "บรรทัดที่ " & i
dt.Rows.Add(dr)
Next
Dim ColA As New DataGridViewTextBoxColumn
With ColA
.ReadOnly = True
.Name = "Index"
End With
Dim ColB As New DataGridViewTextBoxColumn
With ColB
.DataPropertyName = "ItmeName"
.Name = "ItmeName"
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
With dg1
.AutoGenerateColumns = False
.Columns.AddRange(New DataGridViewColumn() {ColA, ColB})
.DataSource = dt
End With
End Sub
Private Sub dg_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) _
Handles dg1.RowPostPaint
'สร้างเลขบรรทัด
Using b As SolidBrush = New SolidBrush(dg1.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), _
sender.DefaultCellStyle.Font, _
b, _
e.RowBounds.Location.X + 40, _
e.RowBounds.Location.Y + 3, _
New StringFormat(StringFormatFlags.DirectionRightToLeft))
End Using
Me.dg1("Index", e.RowIndex).Value = e.RowIndex
End Sub
End Class