none
DataGridView get unmasked content by GetClipboardContent RRS feed

  • Question

  • Hi all,

    I have a datagridview that password in one of its cells and that passwords have been masked by below code:

    private void dgvPassword_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    	try
    	{
    		if (e.ColumnIndex == 2 && e.Value != null)
    		{
    			dgvPassword.Rows[e.RowIndex].Tag = e.Value;
    			e.Value = new String('*', e.Value.ToString().Length);
    		}
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.Message);
    	}
    }

    now when the content loads, the datagridview is able to mask the password with password char (*). 

    but what i would like is when a user use the copy menu on ContextMenuStrip of that password column, it is able to unmask the password, copy the password and paste it anywhere with the original password.

    So far the copy menu of the ContextMenuStrip has below codes, but it only copies the password char (*), so how can i get the unmasked password please:

    internal static void CopyToClipboardWithHeaders(DataGridView _dgv, bool isHeaderIncluded)
    {
    	//Copy to clipboard
    	if (isHeaderIncluded == true)
    	{
    		_dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
    	}
    	else
    	{
    		_dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
    	}
    	DataObject dataObj = _dgv.GetClipboardContent();
    	if (dataObj != null)
    	{
    		Clipboard.SetDataObject(dataObj);
    	}
    }

    I have tried to manipulate the dataObj, again it only got set with the password char (*).

    Thanks

    Andie.

    • Moved by CoolDadTx Monday, November 19, 2018 3:27 PM Winforms related
    Monday, November 19, 2018 4:17 AM

Answers

  • Hi,

    >> … so how can i get the unmasked password …

    Maybe you can use an array to save the password, just like this:

        static string[] pwd = new string[100];
        private void dgvPassword_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2 && e.Value != null)
                {
                    pwd[e.RowIndex] = e.Value.ToString();
                    dgvPassword.Rows[e.RowIndex].Tag = e.Value;
                    e.Value = new String('*', e.Value.ToString().Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(pwd[dgvPassword.CurrentCell.RowIndex]);
        }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndieDu Wednesday, November 21, 2018 1:17 AM
    Tuesday, November 20, 2018 6:36 AM
    Moderator

All replies

  • anyone has any clues please.....
    Tuesday, November 20, 2018 5:27 AM
  • Hi,

    >> … so how can i get the unmasked password …

    Maybe you can use an array to save the password, just like this:

        static string[] pwd = new string[100];
        private void dgvPassword_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2 && e.Value != null)
                {
                    pwd[e.RowIndex] = e.Value.ToString();
                    dgvPassword.Rows[e.RowIndex].Tag = e.Value;
                    e.Value = new String('*', e.Value.ToString().Length);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(pwd[dgvPassword.CurrentCell.RowIndex]);
        }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndieDu Wednesday, November 21, 2018 1:17 AM
    Tuesday, November 20, 2018 6:36 AM
    Moderator
  • thanks a lot Stanly, it did work.

    and actually we can just SetText to the value of the cell, that way we dont need to declare a pwd array, as per below:

    private void cmsCopyValueOnly_Click(object sender, EventArgs e)
    {
    	try
    	{
    		Clipboard.SetText(dgvPassword.CurrentCell.Value.ToString());
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.Message);
    	}
    	//CopyFromDataGridView(false);
    }

    Wednesday, November 21, 2018 1:16 AM