PDA

View Full Version : نیاز به DataGrid سفارشی



ESG_Esfahan
سه شنبه 13 تیر 1385, 00:04 صبح
سلام
من می خوام کاری کنم که در DataGrid هر Cell قابل انتخاب نباشه و مثل ListView سطر ها به صورت کامل انتخاب بشه.
ممنون میشم اگه من را راهنمایی کنید.

shahab_ss
سه شنبه 13 تیر 1385, 14:26 عصر
Datagridview.sellectionmode = fullrow select

ESG_Esfahan
چهارشنبه 14 تیر 1385, 00:31 صبح
سلام شهاب جان
من با Vs2003 کار می کنم و تا حالا هم این چیزهایی که تو می گی را ندیدم .فکر کنم شما برای 2005 این راه حل را گفتی . تو 2003 چیکار میشه کرد؟

Happy_davood
چهارشنبه 14 تیر 1385, 05:44 صبح
به Property Panel اگر دقت کنید و اگر اشتباه نکرده باشم یک پراپرتی به نام SElectionMode خواهد داشت .
البته من مدتی هست که 2003 کار نکردم و شاید اسم Property درست نباشه ولی یقیناً هست .
این Property حتی در Gridهای خیلی قدیمی تر مثل VSFlexGrid هم هست چه برسد به 2003 .

ESG_Esfahan
پنج شنبه 15 تیر 1385, 23:11 عصر
یا من خیلی ...
یا شما از چیز دیگه ای صحبت می کنید !!!
بابا پس این پراپرتی که شما می گید کجاست؟؟؟

M.GhanaatPisheh
جمعه 16 تیر 1385, 17:28 عصر
دوست عزیزم،
توی رویداد MouseUp مربوط به DataGrid کد زیر رو اضافه کنید :

dataGrid1.Select( dataGrid1.CurrentRowIndex);

ESG_Esfahan
شنبه 17 تیر 1385, 00:28 صبح
خیلی ممنون
ولی من قبلا این کار را کردم مشکلش اینکه که کل ردیف انتخاب میشه ولی بازم خونه ای که روی اون کلیک میشه با بقیه خونه ها فرق داره و رنگش با بقیه خونه های اون ردیف فرق می کنه.

حالا یه سوال دیگه که بی ربط به این موضوع هم نیست اینکه آیا میشه در برنامه یک Cell گرید را انتخاب کرد یا نه ؟

M.GhanaatPisheh
شنبه 17 تیر 1385, 02:30 صبح
مشکلش اینکه که کل ردیف انتخاب میشه ولی بازم خونه ای که روی اون کلیک میشه با بقیه خونه ها فرق داره و رنگش با بقیه خونه های اون ردیف فرق می کنه.
چه فرقی؟
من فرقی احساس نکردم.
اگر دقیقا همین کدی که نوشتم ، دقیقا توی همین رویداد بنویسید ، بدون مشکل اجرا می شه.
تنها در صورتی ممکنه یک Cell تفاوت رنگ داشته باشه که مثلا توی رویداد Load مربوط به Form بنویسید :

dataGrid1.Select(0);

لیتک زیر رو بررسی کنید :
http://www.c-sharpcorner.com/UploadFile/suparba/MULTIDATAGRIDROW107282005111750AM/MULTIDATAGRIDROW1.aspx?ArticleID=80c02377-45c5-4e5f-a445-6e8600a913d2


واما در مورد سوال دوم :

به طور پیش فرض DataGrid همین حالت رو داره دوست من.

در ضمن رویداد CurrentCellChanged هم می تونه بهتون کمک کنه.

ESG_Esfahan
یک شنبه 18 تیر 1385, 00:24 صبح
می بخشید من قبلا این کد را در رویداد CurrentCellChanged می نوشتم .
ولی حالا یه مشکل دیگه اینکه اگه با صفحه کلید بین ردیف ها حرکت کنیم اگه کد را در رویداد CurrentCellChanged بنویسیم همون مشکلی پیش می یاد که قبلا گفتم تو رویداد KeyUp هم نمیشه نوشت چون وقتی داخل یک Cell هستیم و کلیدی را می زنیم این رویداد اجرا نمیشه .
به نظر شما حالا باید چکار کرد؟

و در مورد انتخاب کردن یک Cell من از کد زیر اسفاده کردم نظر شما چیه؟
;(DataGridCell c=new DataGridCell(5,0
; dataGrid1.CurrentCell=c
از توجه شما خیلی ممنونم.

shahab_ss
یک شنبه 18 تیر 1385, 06:38 صبح
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1585&lngWId=10

M.GhanaatPisheh
یک شنبه 18 تیر 1385, 15:12 عصر
پروژه ای معرفی کردید ، دقیقا از همین تکنیکی که اشاره کردم استفاده می کنه.
در حقیقت DataGrid محلی رو توسعه داده و از همون Eventی که پیشنهاد دادم استفاده کرده.
اما یه مشکل وجود داره و اونم اینه که اینDataGrid هم نیاز دوم شما (حرکت بوسیله کلید های جهت دار صفحه کلید) رو بر آورده نمی کنه و بایستی خودتون رویداد CurrentCellChanged رو براش پیاده سازی کنید
کدهای توسعه یافته رو ببینید :


Option Explicit On
Option Strict On

Imports System
Imports System.Windows
Imports System.Windows.Forms

Namespace Components
'''-----------------------------------------------------------------------------
''' Project : Project_DataGrid
''' Class : CDataGrid
'''
'''-----------------------------------------------------------------------------
''' <summary>
''' Inherited DataGrid
''' Now with new futures.
''' </summary>
''' <remarks>This class can not be inheritable.</remarks>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Public NotInheritable Class CDataGrid
Inherits DataGrid

Private m_HtiMouseDown As HitTestInfo '//--- for storeing HitTestInfo
Private m_LastHTI As HitTestInfo '//--- lase selected row
Private m_ControlCollenction As New ArrayList '//--- backup of controlcollection

Private m_FullRowSelect As Boolean '//--- full row select

Sub New()
MyBase.New()
End Sub


'''-----------------------------------------------------------------------------
''' <summary>
''' We overrided event OnMouseDown so we can propertly set Grid.Select()
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim ptPoint As Point = New Point(e.X, e.Y)

'// first, call base event
MyBase.OnMouseDown(e)


Dim htInfo As HitTestInfo = Me.HitTest(ptPoint)

Try
'// check for cell type
If htInfo.Type = HitTestType.Cell Then

m_HtiMouseDown = htInfo

'// check if it will be fullrowselect
If m_FullRowSelect Then
'// set current cell
Me.CurrentCell = New DataGridCell(htInfo.Row, htInfo.Column)
Me.Select(htInfo.Row)
End If

End If

Catch
End Try

End Sub


'''-----------------------------------------------------------------------------
''' <summary>
''' We overrided event OnMouseUp so we can propertly set Grid.Select()
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim ptPoint As Point = New Point(e.X, e.Y)

'// first, call base event
MyBase.OnMouseUp(e)


Dim htInfo As HitTestInfo = Me.HitTest(ptPoint)

Try
'// check for cell type
If htInfo.Type = HitTestType.Cell Then

m_LastHTI = htInfo

'// set current cell
Me.CurrentCell = New DataGridCell(htInfo.Row, htInfo.Column)

'// check if it will be fullrowselect
If m_FullRowSelect Then
'// unselect row that was selected on mouse down
Me.UnSelect(m_HtiMouseDown.Row)
'// select row on witch was mouse released (mouseup)
Me.Select(htInfo.Row)
End If

End If

Catch
End Try

End Sub


'''-----------------------------------------------------------------------------
''' <summary>
''' If we will have fullrowselect
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
<System.ComponentModel.Browsable(False)> _
Public Property FullRowSelect() As Boolean
Get
Return m_FullRowSelect
End Get
Set(ByVal Value As Boolean)
m_FullRowSelect = Value
If Value Then
If Not (m_LastHTI Is Nothing) Then
Me.Select(m_LastHTI.Row)
Else
Try
Me.Select(0)
Catch
End Try
End If
Else
If Not (m_LastHTI Is Nothing) Then
Me.UnSelect(m_LastHTI.Row)
Else
Try
Me.UnSelect(0)
Catch
End Try
End If
End If
End Set
End Property

End Class

'''-----------------------------------------------------------------------------
''' Project : Project_DataGrid
''' Class : CDataGridLabelColumn
'''
'''-----------------------------------------------------------------------------
''' <summary>
''' This is the way how to make column not editable.
''' In this case we just overrides sub 'Edit', and
''' let him do nothing. ( other word, brake on edit )
''' </summary>
''' <history>
''' [jhabjan] 20.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Public Class CDataGridLabelColumn
Inherits DataGridTextBoxColumn

Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
'// do nothing
End Sub

End Class

End Namespace