 |
|
vb.net ต้องการแสดงผลใน Datagrid ให้มี ลำดับแถว ทำไงครับ |
|
 |
|
|
 |
 |
|
for สิครับ
|
 |
 |
 |
 |
Date :
2010-11-06 13:42:32 |
By :
nooknoname |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ของผมใช้
Code (VB.NET)
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()
ลองดูครับ ไม่รู้จะช่วยได้รึป่าวครับ
|
 |
 |
 |
 |
Date :
2010-11-11 10:58:54 |
By :
รามาตูม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมมีวิธี ให้แสดงลำดับของ Row ที่ RowHeader เมื่อบรรทัดใดบรรทัดหนึ่งถูกลบไป ลำดับก็จะยังคงเรียงต่อเนื่อง ไม่ขาดหายไป
แต่ตอนหนี้ผมกำลังล้างเครื่องอยู่ ไม่สามารถเปิด VB.net ได้ สักประมาณ 5 โมงน่าจะเสร็จ แล้วจะมาโพสวิธีให้ เขียนเองสด ๆ กลัวผิด
|
 |
 |
 |
 |
Date :
2010-11-11 13:23:06 |
By :
BASE3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รอแป๊ปนะครับ มีปัญหาต้องลงรอบ 2 แต่มาแน่นอน
|
 |
 |
 |
 |
Date :
2010-11-11 20:23:41 |
By :
BASE3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มาแล้วครับ ถึงจะช้าไปนิด
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
ไม่ได้แยกสีมา ไม่รู้เข้าใจหรือเปล่า ประมาณว่า ให้เรียก Event -- RowPostPaint
จากนั้นให้ Copy คำสั่งชุดที่ต่อจาก สร้างเลขบรรทัด เข้าไป DataGridView ตารางนั้น ๆ ก็จะมีเลขบรรทัดกำกับทุกบรรทัด
ส่วนถ้าต้องการใส่เลขบรรทัดไว้ใน Column ของตาราง ก็ให้ทำที่ Event นี้เหมือนกัน ขั้นตอนคร่าว ๆ คือ
สร้าง Column ใหม่ให้ตาราง หลังจาก Bind ข้อมูลแล้ว
Me.dg1.Columns.Add("ชื่อ", "หัว") -----> DeFault เป็น TextBoxColumn อยู่แล้ว
หลังจากนั้นให้นำคำสั่งชุดนี้ไปใส่ที่ Event RowPostPaint
Me.dg(e.RowIndex, "ชื่อ" หรือ ลำดับแถว).value = e.RowIndex
เท่านี้ก็เรียบร้อยแล้วครับ ถ้าไม่เข้าใจ โพสไว้ พรุ่งนี้จะเข้ามาดู
|
 |
 |
 |
 |
Date :
2010-11-11 22:24:22 |
By :
BASE3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ของส่งให้ใหม่ครับ เพิ่งรู้ว่าสามารถทำสีเหมือน 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
|
 |
 |
 |
 |
Date :
2010-11-11 23:02:10 |
By :
BASE3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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 ขึ้นมาค่ะ
|
 |
 |
 |
 |
Date :
2010-11-12 12:07:12 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โอ้วันนี้ เจ๊ Stupid gurl มาตอบให้แบบเต๊มๆๆ ไม่แซว ด้วย อิอิ
|
 |
 |
 |
 |
Date :
2010-11-12 13:11:06 |
By :
noizeless |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ค่อยจะเต็มเท่าไหร่หรอกค่ะ ไม่ค่อยจะว่าง สงสัยอะไรหรอคะ
|
 |
 |
 |
 |
Date :
2010-11-12 13:55:10 |
By :
blurEyes |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 11 เขียนโดย : blurEyes เมื่อวันที่ 2010-11-12 12:07:12
รายละเอียดของการตอบ ::
... ขอบคุณครับ ทำตามที่บอกได้แล้วครับ.......
ส่วนผมทำแบบนี้ก็ได้เหมือนกัน ไม่รู้จะเป็นไรไหม
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
ขอถามอีกอย่างว่า ถ้าจะแก้ไขบันทัด ที่เราเลือก ผ่านไปทาง textbox พอกดปุ่มแก้ไข ให้cerser เด่งมาที่บันทัดที่เราเลือกตอนแรก
|
 |
 |
 |
 |
Date :
2010-11-12 16:21:13 |
By :
chin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ต้องวนหรอก ให้มันสร้างตอน RowCreated นี่แหละ เขียนครั้งเดียวแล้วก็เรียกใช้ได้ทุก Grid
Code (C#)
public int iRow = 0;
protected void Gridview_RowCreated(object sender, GridViewRowEventArgs e)
{
iRow = e.Row.DataItemIndex + 1;
}
ใช้ item template เรียก <%# iRow %>
<asp:TemplateField HeaderText="No" HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="right" HeaderStyle-Width="25px" >
<ItemTemplate>
<%# iRow %>
</ItemTemplate>
</asp:TemplateField>
ใน Gridview Set ให้
OnRowCreated="Gridview_RowCreated"
|
 |
 |
 |
 |
Date :
2010-11-12 16:57:55 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ต่อไปนี้เป็น CODE
สร้าง WinForm แล้วสร้างตารางชื่อ dg1 ก่อน
หลังจากนั้น Copy โค๊ดชุดนี้ไปแทนที่ทั้งหมดใน Code Editor เพื่อทำการทดลอง
Code (VB.NET)
'สร้าง 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
สุดท้ายนี้ อยากถามว่า คำตอบที่โพสแล้ว สามารถกลับไปแก้ได้หรือไม่ ใครรู้ช่วยตอบที บางทีอยากแก้ไขนิดหน่อยแต่ทำไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2010-11-15 08:42:19 |
By :
BASE3000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณไม่ได้สมัครสมาชิกครับ ไม่สามารถแก้ไขข้อความที่โพสไปแล้วได้ครับ
|
 |
 |
 |
 |
Date :
2010-11-15 10:43:30 |
By :
Amanza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ "BASE3000" มากเลยนะคะ
กำลังทำโปรเจ็คพอดี และกำลังหาวิธีนับจำนวนแถวของ Datagridview พอมาเจอกระทู้นี้
แล้วลองทำตามคำแนะนำของ BASE3000 ได้แล้วค่ะ
ขอบคุณมากๆ เลยค่ะ T^T

|
 |
 |
 |
 |
Date :
2014-11-14 12:44:02 |
By :
Tang-Mae |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษทีคะ ไม่รู้ว่ารูปที่อัพโหลดจะใหญ่ขนาดนี้ >/\<
|
 |
 |
 |
 |
Date :
2014-11-14 12:45:07 |
By :
Tang-Mae |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|