如何在c中传递参数rowindex#

guykilcj  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(318)

我对c和mysql也是新手。
在我上一个关于如何显示包含sql数据的messagebox的问题的回答中,他提到:
调用execute方法时,传入参数rowindex和stringtosearch。
我还是不明白它是怎么工作的。
下面是我的viewmodel

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("Select * from movie_list where id_movie = @id_movie", connection);
            adapter.Fill(dt);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }

}

点击

internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var rowIndex = (string)parameter[0];
        var stringToSearch = (string)parameter[1];
        MessageBox.Show(_vModel.Library.[rowIndex][stringToSearch]);
    }

}

xaml公司

<ScrollViewer>
        <ItemsControl Background="#191919" ItemsSource="{Binding Path=Library}" Margin="130,0,2,0" BorderThickness="0">
            <ItemsControl.ItemTemplate>
            <DataTemplate DataType="viewModels:Card">
                <Button Margin="0,2,0,0" Command="{Binding Path=DataContext.Clicked, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
                    <StackPanel Margin="0,0,0,0">
                            <Grid Margin="5,5,5,5">
                                <Rectangle RadiusX="5" RadiusY="5" Width="150" Height="230">
                                    <Rectangle.Fill>
                                        <ImageBrush  x:Name="myImage" ImageSource="{Binding Path=cover}"/>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <Label Background="#CC000000" Margin="107,10,0,193" Content="{Binding Path=year}" HorizontalContentAlignment="right" Foreground="White"/>
                                <Label Width="150" Background="#CC000000" Margin="0,203,0,0" Content="{Binding Path=title}" HorizontalContentAlignment="Center" Foreground="White"/>
                            </Grid>

                        <Label Visibility="Visible"  Name="lbl" HorizontalContentAlignment="Center" Tag="labl"/>
                    </StackPanel>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
    </ItemsControl>
    </ScrollViewer>

var rowIndex = (string)parameter[0]; 以及 var stringToSearch = (string)parameter[1]; 它表明:
无法对“object”类型的表达式应用带[]的索引
当我改变主意的时候 [rowIndex] 进入 [0] 上面写着:
无法从“object”转换为“string”

kb5ga3dv

kb5ga3dv1#

我认为您需要在dataview中加载所有电影:

public class VModel
{
    public ICommand Clicked { get; set; }
    public DataView Library { get; private set; }
    public VModel()
    {
        DataTable dt = new DataTable();
        using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;"))
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = new MySqlCommand("select * from movie_list", connection);
            adapter.Fill(dt);

        }

        var Library = dt.DefaultView;
        var Clicked = new ClickedCommand(this);
    }
}

单击时,将显示标题:

internal class ClickedCommand : ICommand
{
    private VModel _vModel;
    public ClickedCommand(VModel vModel)
    {
        _vModel = vModel;
    }
    public event EventHandler CanExecuteChanged { add { } remove { } }
    public bool CanExecute(object parameter)
    {
            return true;
    }
    public void Execute(object parameter)
    {
        var id_movie = (int)parameter;
        var rowIndex = id_movie - 1;
        MessageBox.Show(_vModel.Library[rowIndex]["title"].ToString());
    }
}

你可以替换 "title" 按表的其他列名。
此解决方案不使用sql参数。不确定是不是好。

相关问题