 |
|
เปลี่ยนสีของแถวใน datagridview เมื่อเกินวันที่เรากำหนดไว้ 7 วัน |
|
 |
|
|
 |
 |
|
ประมาณนี้ครับ เป็น C# นะเอาไปแปลงเอง
Code (C#)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell = e.Row.Cells[1];
DateTime sendDate = Convert.ToDateTime(cell.Text);
System.TimeSpan diff1 =DateTime.Now- sendDate ;
if (diff1.Days > 7)
{
cell.ForeColor = Color.Red;
}
}
}
|
 |
 |
 |
 |
Date :
2017-12-08 15:09:28 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูโค้ดในส่วนของ ASP หน่อยครับ
|
 |
 |
 |
 |
Date :
2017-12-08 15:56:03 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาง่ายๆ ตอนนี้คุณทำ Window App หรือ เว็บครับ
|
 |
 |
 |
 |
Date :
2017-12-08 16:09:29 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
For i As Integer = 0 To DataGridView1.RowCount - 1
If (Convert.ToDateTime(DataGridView1(4, i).Value.ToString()) - DateTime.Now).Days >7 Then
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Next
End Sub
ยังไม่ลองรันดูนะครับ
หลักการคือ ใช้งาน event DataBindingComplete
ตรวจแต่ละแถวว่าวันที่ มากกว่า รึไม่ถ้ามากกว่าก็เปลี่ยนเป็นสีแดง ครับ
|
 |
 |
 |
 |
Date :
2017-12-08 16:58:22 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองไม่ต้องเช็คเงื่อนไข
แล้วใส่แค่คำสั่งเปลี่ยนสีดูครับ
ว่ามันทำงานได้ไหม
|
 |
 |
 |
 |
Date :
2017-12-12 11:30:32 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเช็คหลัง if ดู ว่ามันเข้าเงื่อนไขไม๊ นะ
Code (VB.NET)
Private Sub AppliedStyleGrid()
For Each gridRow As DataGridViewRow In datagrid_bill.Rows
Dim deliveryDate As DateTime = Convert.ToDateTime(gridRow.Cells("claimsend_dateout").Value.ToString())
If (deliveryDate - DateTime.Now).Days > 3 Then
MessageBox.Show("เช็คสี")
gridRow.DefaultCellStyle.BackColor = Color.Red
Else
MessageBox.Show("เช็ค Days" & (deliveryDate - DateTime.Now).Days )
End If
Next
End Sub
หลักการคือ ไล่เช็คไปแต่ละจุดจะได้รู้ว่า ติดตรงไหนนะครับ
|
 |
 |
 |
 |
Date :
2017-12-12 12:18:41 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รู้แล้วใช่ไม๊ครับ ว่า มันติดตรงไหน
ต่อไปก็
Code (VB.NET)
Private Sub AppliedStyleGrid()
For Each gridRow As DataGridViewRow In datagrid_bill.Rows
Dim deliveryDate As DateTime = Convert.ToDateTime(gridRow.Cells("claimsend_dateout").Value.ToString())
If (deliveryDate - DateTime.Now).Days > 3 Then
MessageBox.Show("เช็คสี")
gridRow.DefaultCellStyle.BackColor = Color.Red
Else
MessageBox.Show("เช็ค Days " & deliveryDate & " Def = " & (deliveryDate - DateTime.Now).Days )
End If
Next
End Sub
|
 |
 |
 |
 |
Date :
2017-12-12 13:05:08 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรงการคำนวณจำนวนวัน
ลองเปลี่ยนไปใช้คำสั่ง datediff แทนดูครับ
|
 |
 |
 |
 |
Date :
2017-12-12 13:35:10 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
ออกตัวก่อนนะครับว่า มือใหม่เหมือนกันฮาๆๆ ได้จารย์หลายท่านในนี้ช่วยสอนโดยเฉพาะจารย์ tor สุดยอดมากๆ ครับ
วีิธีผมมันจะดาดๆ นะครับลองเอาไปใช้แก้ขัดก่อนครับ
ส่วน code GridColor Copy ของจารย์ tor มาครับ ผมลองเอามาปรับแต่งใช้งานได้ 100 % ครับ ^_^
select *, Datediff(day,Startdate,Enddate) as Waranty--เอา Columnนี้ไป check From table --ทำDatediff ในนี้
Code (VB.NET)
dim objConn as new sqlconnection
dim DA as new sqldataAtapter
dim DT as new Datatable
dim sqltem as string
dim strConn as string = "server=host;database=db;trusted_connection=true;"
page_load
rangeWaranty()
MyDatagrid ()
end sub
private sub MyDatagrid 'อันนี้เขียนสดนะครับผิดพลาด ขอ อภัยครับ พอดีลืมเอา notebook มา
using objConn as new sqlconnect(strconn)
objConn.open
sqltem = "select *, Datediff(day,Startdate(column),Enddate(column)) as Waranty From table"
using DA as new sqldataAdapter
DT = New DATAtable
DA.Fill(DT)
DataGridView1.datasource = DT
end using
end sub
private sub rangeWaranty()
For i As Integer = 0 To DataGridView1.RowCount - 1
If Convert.ToInt16(DataGridView1(2, i).Value) < 3 Then
DataGridView1(2, i).Style.BackColor = Color.Red
End If
Next
ลองดูนะครับ Error ป่าวไม่รู้ 555
|
 |
 |
 |
 |
Date :
2017-12-12 13:50:28 |
By :
adminliver |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเช็คที่ dataGridView ครับ
Code (VB.NET)
Private Sub dataGridView1_DataBindingComplete(ByVal sender As Object, _
ByVal e As DataGridViewBindingCompleteEventArgs) _
Handles dataGridView1.DataBindingComplete
MessageBox.Show("เช็ค dataGridView ")
End Sub
ให้เกตุสังว่า ระหว่าง เรียกมาแบบเดี่ยวๆ กับเรียกผ่าน เมนฟอร์ม มีข้อแตกต่างกันไม๊
|
 |
 |
 |
 |
Date :
2017-12-13 11:24:05 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วิธีที่บอกไว้ว่ามันง่ายๆก็คือใช้ DataBindingComplete นี่แหละครับ 555
เพราะยังไงก็ต้องมาจบที่ DataBindingComplete อยู่แล้ว
เขียนโค้ดใน DataBindingComplete ถือเป็นการแก้ที่ถูกต้องเลยครับ
สามารถลดโค้ดโดยการตัด AppliedStyleGrid() ออกเหลือแค่ datagrid_bill_DataBindingComplete ได้เลย
|
 |
 |
 |
 |
Date :
2017-12-13 15:00:14 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|