如何更新 SfDataGrid 控件中的特定单元格?

问题描述 投票:0回答:1

我在 .NET MAUI 应用程序中使用 Syncfusion 的数据网格控件,现在我想更新用户单击的单个单元格。在 XAML 代码中我有这个:

<syncfusion:SfDataGrid x:Name="PasswdVw" ItemsSource="{Binding PassWrd}"
    SelectedRow="{Binding SelPassRws, Mode=TwoWay}" SelectionMode ="Multiple"
    NavigationMode="Row"
    HeaderRowHeight="35" CellLongPress="PasswdVw_CellLongPress">
       
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:DataGridTextColumn MappingName="PassId" Visible="False"/>
        <syncfusion:DataGridTemplateColumn MappingName="PassTitle" HeaderText="Title" 
            CellPadding="3" HeaderTextAlignment="Center"  ColumnWidthMode="FitByCell">
            <syncfusion:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <local:HyperlinkLabel Text="{Binding PassTitle}"
                        BackgroundColor="White"
                        Url="{Binding PassUrl}"/>
                </DataTemplate>
            </syncfusion:DataGridTemplateColumn.CellTemplate>
        </syncfusion:DataGridTemplateColumn>

        <syncfusion:DataGridTextColumn MappingName="PassUsrname" HeaderText="User Name"
            HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
        <syncfusion:DataGridTextColumn MappingName="PassPassword" HeaderText="Password"
            HeaderTextAlignment="Center" Width="150"/>
        <syncfusion:DataGridTextColumn MappingName="PassUrl" Visible="False"/>
        <syncfusion:DataGridTextColumn MappingName="PassGrpName" HeaderText="Group"
            HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
        <syncfusion:DataGridTextColumn MappingName="PassNotes" HeaderText="Notes"
            Width="100" HeaderTextAlignment="Center"/>
    </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

我的 CellLongPress 事件的幕后代码是:

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {
        var passData = e.RowData as PasswrdInfo;
        string tmpPass, newPass;
        using(SqlConnection c = new SqlConnection(App.ConnStr))
        {
            string query = "select password from PassWrds where Id = " + passData.PassId;
                
            using(SqlCommand cmd = c.CreateCommand())
            {
                c.Open();
                tmpPass = cmd.ExecuteScalar().ToString();
                c.Close();
            }
        }
    
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);
            
    }
}

所以现在我想将单击的单元格设置为

newPass

我尝试了

PasswdVw[e.RowColumnIndex.RowIndex].Cell[3]
和其他类似的东西,以及玩弄
e.RowColumnIndex.RowIndex
e.RowColumnIndex.ColumnIndex
但我只是不断收到错误。

如何将单击的单元格设置为名为

newPass
的变量?

c# xaml maui syncfusion sfdatagrid
1个回答
0
投票

如果你的

PasswrdInfo
类是 observable,假设它看起来像这样:

public partial class PasswrdInfo : ObservableObject
{
    [ObservableProperty]
    private int passId;

    [ObservableProperty]
    private string passTitle;

    [ObservableProperty]
    private string passUsrname;

    [ObservableProperty]
    private string passPassword;

    // etc.
}

您可以简单地将

newPass
分配给
passData.PassPassword
:

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {
        var passData = e.RowData as PasswrdInfo;
        string tmpPass, newPass;

        // skipping some code for brevity
    
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);

        // if PasswrdInfo is observable, this should suffice
        passData.PassPassword = newPass;
    }
}

这应该会自动更新 SfDataGrid 的选定单元格中的数据。

© www.soinside.com 2019 - 2024. All rights reserved.