在C#中的数据表中掩盖单列

时间:2018-06-01 13:03:41

标签: c#

我有在datview中查看的数据表。现在我需要掩盖" AdminNumber"的前6位数字。列内容为" XXXXXX895"。

DataTable输入:

public function update($firstname, $lastname, $postaladdress, $mobilephone, $officephone, $personalemail, $officialemail, $nationality, $city, $identity, $id_number, $gender, $dob, $id)
{
    try
    {
        $stmt=$this->db->prepare
        ("UPDATE clients SET
                 firstname      =:firstname, 
                 lastname       =:lastname, 
                 postaladdress  =:postaladdress, 
                 mobilephone    =:mobilephone,
                 officephone    =:officephone,
                 personalemail  =:personalemail,
                 officialemail  =:officialemail,
                 nationality    =:nationality,
                 city           =:city,
                 identity       =:identity,
                 id_number      =:id_number,
                 gender         =:gender,
                 dob            =:dob
          WHERE client_id=:id");

        $stmt->bindparam(":firstname",$firstname);
        $stmt->bindparam(":lastname",$lastname);
        $stmt->bindparam(":postaladdress",$postaladdress);
        $stmt->bindparam(":mobilephone",$mobilephone);
        $stmt->bindparam(":officephone",$officephone);
        $stmt->bindparam(":personalemail",$personalemail);
        $stmt->bindparam(":officialemail",$officialemail);
        $stmt->bindparam(":nationality",$nationality);
        $stmt->bindparam(":city",$city);
        $stmt->bindparam(":identity",$identity);
        $stmt->bindparam(":id_number",$id_number);
        $stmt->bindparam(":gender",$gender);
        $stmt->bindparam(":dob",$dob);
        $stmt->bindparam(":id",$id);
        $stmt->execute();

        return true;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();  
        return false;
    }
}

我需要数据表输出:

AdminNumber   Name
245637895     sham
124563789     Ram

我试过了,但是我得到格式化字符串选项:

  gridview.DataSource = dtSource;
       gridview.Columns[0].FormatString = "######";

我输错了

AdminNumber  Name
XXXXXX895    Sham
XXXXXX789    Ram

2 个答案:

答案 0 :(得分:4)

您可以使用CellFormatting事件:

private void gridview_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{
    if (e.ColumnIndex == 0 && e.Value != null) 
    {
        string adminNumber  = e.Value.ToString();
        int length = Math.Min(6, adminNumber.Length);
        adminNumber =  new string('X', length) + adminNumber.Substring(length);
        e.Value = adminNumber;
        e.FormattingApplied = true;
    }
}

答案 1 :(得分:0)

您可以使用直接在数据库中选择查询,如下所示:

select stuff(column_name, 1, 6, '######') from table_name

然后在gridview中绑定此数据表。