如何使用按钮加载datagrid绑定信息?

时间:2019-05-28 11:24:44

标签: c#

我希望在用户单击“加载”按钮时填充或加载DataGrid

这是我的网格:

  <Grid>
     <DataGrid Name="test" ItemsSource="{Binding Items}"  AutoGenerateColumns="True" Margin="0,0,286.4,190.6" />
     <Button Content="Load" Command="{Binding cmdReset_Click}" Margin="450,0,165.4,702.6"/>   
  </Grid>

这是我的ViewModel

 namespace test.ViewModel
 {

    class ViewModel : INotifyPropertyChanged
    {

        public DelegateCommand cmdReset_Click { get; }

        public ViewModel()
        {
            cmdReset_Click = new DelegateCommand(Reset_Click);
            Items = GetData();
        }

        public class Log
        {
            public int ID { get; set; }
            public string PDO { get; set; }
            public string LOCATION { get; set; }
            public string HOSTAME { get; set; }
            public int SEVERITY { get; set; }
            public DateTime TIMESTAMP { get; set; }
            public string MESSAGE { get; set; }

        }

        public List<Log> Items { get; set; } = new List<Log>();

        private List<Log> GetData()
        {                
                List<Log> logs = new List<Log>();               
                    logs.Add(new Log()
                    {
                        PDO = "TEST",
                        ID = "TEST",
                        //LOCATION = "TEST",
                        HOSTAME = "TEST",
                        SEVERITY = "TEST",
                        TIMESTAMP = "TEST",
                        MESSAGE = "TEST",
                    });
                }
            return logs;
        }
        private void Reset_Click()
        {
            Items = GetData();
        }
    }
}

现在,我在运行程序后得到列表信息。单击按钮后,我需要获得什么信息

3 个答案:

答案 0 :(得分:0)

public DelegateCommand cmdReset_Click { get; }

public ViewModel()
{
    cmdReset_Click = new DelegateCommand(Reset_Click);
    Items = GetData();
}

public class Log
{
    public int ID { get; set; }
    public string PDO { get; set; }
    public string LOCATION { get; set; }
    public string HOSTAME { get; set; }
    public int SEVERITY { get; set; }
    public DateTime TIMESTAMP { get; set; }
    public string MESSAGE { get; set; }

}

public List<Log> Items {get; set;} = new List<Logs>();


private GetData()
{
   List<Log> logs = new List<Log>();
        logs.Add(new Log()
            {
                PDO = "test",
                ID = "number",
                HOSTAME = "test",
                SEVERITY = "number",
                TIMESTAMP = "nate",
                MESSAGE ="number",
            });
        }
        return logs;

}

private void Reset_Click()
{
    Items = GetData();
}

答案 1 :(得分:0)

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }

    private List<Log> GetData()
    {
        List<Log> logs = new List<Log>();
        logs.Add(new Log()
        {
            PDO = "test",
            ID = 100,
            HOSTAME = "test",
            SEVERITY = 5,
            TIMESTAMP = DateTime.Now,
            MESSAGE = "number",
        });
        return logs;
    }
}

public class Log
{
    public int ID { get; set; }
    public string PDO { get; set; }
    public string LOCATION { get; set; }
    public string HOSTAME { get; set; }
    public int SEVERITY { get; set; }
    public DateTime TIMESTAMP { get; set; }
    public string MESSAGE { get; set; }
}

答案 2 :(得分:0)

对于那些有相同问题的人..这就是答案...

查看:

<Grid>
     <DataGrid Name="test" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}"  AutoGenerateColumns="True" />
     <Button Content="Load" Command="{Binding cmdReset_Click}"/>   
</Grid>

ViewModel:

public DelegateCommand cmdReset_Click { get; }

public LoggingViewModel(){
  cmdReset_Click = new DelegateCommand(Reset_Click);
}

public class Log{
   public int ID { get; set; }
   public string PDO { get; set; }
   public string LOCATION { get; set; }
   public string HOSTAME { get; set; }
   public int SEVERITY { get; set; }
   public DateTime TIMESTAMP { get; set; }
   public string MESSAGE { get; set; }
}

public List<Log> Items { get; set; } = new List<Log>();

private List<Log> GetData(){                
   List<Log> logs = new List<Log>();               
   logs.Add(new Log(){
     PDO = "TEST",
     ID = "TEST",
     //LOCATION = "TEST",
     HOSTAME = "TEST",
     SEVERITY = "TEST",
     TIMESTAMP = "TEST",
     MESSAGE = "TEST",
    });
  }
    return logs;
}

private void Reset_Click(){
  Items = GetData();
  this.OnPropertyChanged("Items");
}