Caliburn Micro将Edit按钮添加到每行的DataGrid中

时间:2018-04-23 12:56:03

标签: c# wpf data-binding datagrid caliburn.micro

我在我的项目中使用Caliburn.Micro。我正在尝试使用Caliburn.Micro实现CRUD操作。我已完成所有部分,但我在编辑数据时遇到问题。我想在每行的DataGrid中添加按钮,这样当用户点击编辑按钮时,可以在数据库中编辑和更新行值。

这是我的代码

ShellView.xaml

<Button x:Name="ButtonUpdate" Width="100" Height="30" Content="Update"/>
    <DataGrid  x:Name="Empdata2"  AutoGenerateColumns="False" ItemsSource="{Binding Path= Empdata}" SelectedItem="{Binding selecteditem}" Height="162" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding Path=id}"/>
            <DataGridTextColumn Header=" First Name" Binding="{Binding Path= fname}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding Path=lname}"/>
            <DataGridTemplateColumn Header="delete">
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <Button Content="Delete" 
                                cal:Bind.Model="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" x:Name="DeleteButton" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Edit">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Edit" 
                                x:Name="EditButton"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

ShellViewModel.cs

public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
{
    private ObservableCollection<Person> _empdata = new ObservableCollection<Person>();

    public ObservableCollection<Person> Empdata
    {
        get { return _empdata; }
        set
        {
            _empdata = value;
            NotifyOfPropertyChange(() => Empdata);
        }
    }

    Person model = new Person();

    Auth obj = new Auth();
    public void ButtonUpdate()
    {
        try
        {
            DataSet d = new DataSet();


            d = obj.updateuser();
            Empdata.Clear();
            for (int i = 0; i < d.Tables[0].Rows.Count; i++)
                Empdata.Add(new Person
                {
                    id=Convert.ToInt32(d.Tables[0].Rows[i][0]),
                    fname = d.Tables[0].Rows[i][1].ToString(),
                    lname = d.Tables[0].Rows[i][2].ToString(),
                });
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private Person _selecteditem;
    public Person selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            NotifyOfPropertyChange(() => selecteditem);
        }
    }

    string sel;

    public void DeleteButton()
    {
        Auth obj = new Auth();
        sel = selecteditem.fname.ToString();
        bool find = obj.deluser(sel);
        if (find == true)
        {
            ButtonUpdate();
        }
    }
    public void EditButton(){
        Auth obj = new Auth();
        sel = selecteditem.fname.ToString();
        ler = selecteditem.lname.ToString();
        bool edit = obj.edituser(sel,ler);
        if (edit == true)
        {

        }
    }

Person.cs

public class Person:PropertyChangedBase

{

public string FirstName { get; set; }
public string LastName { get; set; }

private string Firstname;
public string fname
{
    get { return Firstname; }
    set
    {
        Firstname = value;
        NotifyOfPropertyChange(() => fname);
    }
}
private string Lastname;
public string lname
{
    get { return Lastname; }
    set
    {
        Lastname = value;
        NotifyOfPropertyChange(() => lname);
    }
}

Auth.cs

public class Auth
{
    SqlConnection conn = new SqlConnection(@"Data Source = a; Initial Catalog = ab; Persist Security Info=True;User ID = s; Password=123");

public DataSet updateuser()
{
    try
    {

        conn.Open();
        SqlCommand comm = new SqlCommand("Select * from [add]", conn);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(comm);
        da.Fill(ds);
        conn.Close();
        return ds;

    }
    catch (Exception ex)
    {
        throw ex;
    }
}
public bool deluser(string name)
{
    try
    {
        conn.Open();
        SqlCommand comm = new SqlCommand("Delete from [add] where firstname=" + name + "", conn);
        comm.ExecuteNonQuery();
        conn.Close();
        return true;
    }
    catch(Exception ex)
    {
        throw ex;

    }
}
public bool edituser(string frname,string lrname)
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("Update [add] set firstname='" + frname + "' where lastname='" + lrname + "'", conn);
            comm.ExecuteNonQuery();
            conn.Close();

            return true;
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }

0 个答案:

没有答案