0

I'm using a data grid control from Syncfusion in my .NET MAUI application, and now I want to update a singe cell that the user clicked on. In the XAML code I have this:

<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>

My behind code for the CellLongPress event is:

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);
            
    }
}

So now I'm at a point where I want to set the cell that was clicked to the newPass.

I tried PasswdVw[e.RowColumnIndex.RowIndex].Cell[3] and other things similar as well as playing around with e.RowColumnIndex.RowIndex and e.RowColumnIndex.ColumnIndex but I just keep getting errors.

How can I set the cell that was clicked to the variable named newPass?

Julian
  • 5,290
  • 1
  • 17
  • 40
BobG
  • 9
  • 1

2 Answers2

1

Based on the details you've provided, it appears that the problem you're encountering revolves around assigning the newPass value to a PassPassword property within the Model Class. The DataGridCell value relies on properties from the model class to be updated. If your model class is Observable or implements INotifyPropertyChanged, simply assigning the updated value to the property within the model class is sufficient. This will automatically trigger the reflection of the changes in the DataGrid.

Code snippet:

private void PasswdVw_CellLongPress(object sender, DataGridCellLongPressEventArgs e)
{
    if(e.RowColumnIndex.ColumnIndex == 3)
    {  
        newPass = Crypto.Decrypt(App.secretKey, tmpPass);    
        passData.PassPassword = newPass;
    }
}
0

In case that your PasswrdInfo class is observable, let's say it looks something like this:

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

    [ObservableProperty]
    private string passTitle;

    [ObservableProperty]
    private string passUsrname;

    [ObservableProperty]
    private string passPassword;

    // etc.
}

you could simply assign newPass to 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;
    }
}

This should automatically update the data in the selected cell of the SfDataGrid.

Julian
  • 5,290
  • 1
  • 17
  • 40