2010-04-29 4 views
4

사용자가 파일을 선택하고 해당 파일을 여는 데 필요한 자격 증명을 제공하는 응용 프로그램을 만들고 있습니다. 이를 위해 gridview에 세 개의 열을 만들었습니다.
사용자가 암호 열에 암호를 입력합니다.
표시하고 싶습니다. 암호 유형의 텍스트 상자를 만들 수있는 것처럼 문자 대신에 *을 표시하고 싶습니다.
나는 GridView_CellClick 이벤트에이 코드를 시도 : Gridview에서 암호 유형의 열을 만드는 방법은 무엇입니까?

if (GridView.Columns[e.ColumnIndex].HeaderText == "Password") 
{ 
    txtPassword[e.RowIndex] = new TextBox(); 
    txtPassword[e.RowIndex].Name = "txtPassword"+e.RowIndex; 
    txtPassword[e.RowIndex].PasswordChar = '*'; 
    txtPassword[e.RowIndex].Visible = true; 
    txtPassword[e.RowIndex].TextChanged += new  

    if (GridView.CurrentCell.Value == null) 
     txtPassword[e.RowIndex].Text = ""; 
    else 
     txtPassword[e.RowIndex].Text = GridView.CurrentCell.Value.ToString(); 

    txtPassword[e.RowIndex].Location = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Location; 

    txtPassword[e.RowIndex].Size = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Size; 

    txtPassword[e.RowIndex].Visible = true; 
    txtPassword[e.RowIndex].Focus(); 
} 

그러나 위의 솔루션 문자

가 표시됩니다.
이 문제를 어떻게 해결할 수 있습니까?

답변

1

나는 그것이 EditingControlShowing 이벤트를 처리하는 일을해야한다고 생각하고 처리기에 다음 코드를 추가합니다

if(e.ColumnIndex == 2) 
{ 
    TextBox tb = e.Control as TextBox; 
    if (tb != null) 
    { 
     tb.PasswordChar = '*'; 
    } 
} 

CellFormatting 이벤트 처리기 코드 :

if(e.ColumnIndex = 2) 
{ 
    if(e.Value != null) 
    { 
     e.Value = New string("*", e.Value.ToString().Length); 
    } 
} 

그리고이 이벤트 전자의

이 있어야한다 ColumnIndex 속성 :

+0

내가 기본적으로해야합니다 e.Control – Preeti

+0

은'EditingControlShowing'의 핸들러를 찾는 있지 않다'DataGridViewEditingControlShowingEventArgs과 같이 e' 두 번째 매개 변수, 그 중 하나입니다. –

+0

columnindex는 DataGridViewEditingControlShowingEventArgs의 멤버가 아닙니다. 확인할 수 있습니까? – Preeti

0

한 가지 해결책은 자신의 유형의 셀을 만들고이 유형을 암호 열에 할당하는 것입니다. 예를 들어 표준 TextBoxPassword 만 추가하면 원하는대로 작동합니다.

MSDN에는 사용 가능한 소스 코드에 대한 자세한 설명이 있습니다.

CellTemplate의 원 종류를 만들어야합니다.

0

사용자 지정 셀과 열을 만들고 암호 마스크가있는 텍스트 상자를 편집 컨트롤로 사용할 수 있습니다.

편집 모드를 종료 할 때 일반 텍스트를 가져 오는 것을 막기 위해 실제 암호 값을 셀의 별도 속성에 저장할 수 있으며 GetFormattedValue 이벤트 (나는 믿을 만합니다) "*"문자로 일반 디스플레이를가립니다. 당신이 뭘 하려는지에 대한 자세한 내용은

Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object 
     Dim strVal As String 

     strVal = New String(CChar("*"), value.ToString.Length) 

     Return MyBase.GetFormattedValue(strVal, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context) 

    End Function 
    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _ 
    ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle) 

     MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _ 
             dataGridViewCellStyle) 

     Dim ctl As PasswordTextBoxEditingControl = _ 
      CType(DataGridView.EditingControl, PasswordTextBoxEditingControl) 

     If IsDBNull(Me.Value) Then 
      ctl.Text = "" 


     Else 
      ctl.Text = CType(Me.Value, String) 
      ctl.PasswordChar = "*" 
      ctl.Mask = "*" 
     End If 

    End Sub 

이 방문 : http://www.vbforums.com/showthread.php?t=554744

관련 문제