【问题标题】:Silverlight Data Context, datagridSilverlight 数据上下文,数据网格
【发布时间】:2016-06-04 03:23:21
【问题描述】:

我有数据网格视图,其中是来自 sql 表的数据,而且每一行都有显示子窗口的按钮。在这个子窗口中,很少有 texbox、组合框和数据选择器,我需要从我的数据网格中填充这些控件数据。我使用方法来填充下面的数据网格。

    public ObservableCollection<MyClass> ReadUpdate(int id_update)
{
ObservableCollection<MyClass> result = new ObservableCollection<MyClass>();
string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataReader dr;
SqlConnection conn = new SqlConnection(nwConn);
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.CommandText = "Insert_Update";
cmd.Parameters.AddWithValue("@id_update", id_update);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
MyClass lin = new MyClass();

lin.id = dr.GetInt32(1);
if (!dr.IsDBNull(2)) lin.other = dr.GetString(2);
if (!dr.IsDBNull(3)) lin.barkod = dr.GetString(3);
if (!dr.IsDBNull(4)) lin.pw = dr.GetInt32(4);

result.Add(lin);
}
dr.Close();
return result;

}
catch (SqlException e)
{
MyClass lin = new MyClass();
lin.other = e.Message;

result.Add(lin);
return result;

}
finally
{
conn.Close();

};
}

我的班级:

public class PIS
{
public int ID { get; set; } 
public int PW { get; set;}
public string other { get; set;}
public string barkod { get; set;}
 }

我的数据网格和插入数据的方法(工作正常)

private PIS pis_update;

 public Home() 
        {
            InitializeComponent();            
            webService = new ServiceReference1.Service1Client();
            webService.ReadPismaCompleted += WebService_ReadPismaCompleted;
            webService.ReadPismaAsync(0);

        }

        private void WebService_ReadPismaCompleted(object sender, ServiceReference1.ReadPismaCompletedEventArgs e)
        {
                if (e.Result != null)
                {             
                dataGridPisma.ItemsSource = e.Result;  

            }
        }

我的按钮:

private void btnUpdate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
             pismo_update = (PIS)((Button)sender).DataContext;  

            ChildWindow_Update childWindow_update = new ChildWindow_Update();


            childWindow_update.DataContext = ((PIS)((Button)sender).DataContext).Id_Pis;

            childWindow_update.Closed += ChildWindow_Update_Closed;
            childWindow_update.Show();

        }

        private void ChildWindow_Update_Closed(object sender, EventArgs e)
        {
            if (((ChildWindow_Update)sender).DialogResult.Value)
            {
                webService.ReadPismaAsync(0);
            }
        }

我的xml:

<TextBox x:Name="textBox1_other" Text="{Binding Path= other}"/>

<TextBox x:Name="textBox2_barkod" Text="{Binding Path= barkod}"/>

 <ComboBox x:Name="comboBoxPW" HorizontalAlignment="Left" Margin="40,117,0,0" VerticalAlignment="Top" Width="366"   
            SelectedItem="{Binding ReadComboboxPW}"   DisplayMemberPath="PW"  SelectedValuePath="IDPW"  />

单击按钮后我尝试发送数据上下文我的类并插入到子窗口中的控件中,它不起作用

【问题讨论】:

    标签: c# xaml silverlight datagrid


    【解决方案1】:

    伙计,我建议你学习一下你的英语,它比我的还差,这可以解释为什么人们不急于帮助你。

    无论如何,我不确定我是否完全理解你的目标,还是这条线:

    childWindow_update.DataContext = ((PIS)((Button)sender).DataContext).Id_Pis;
    

    很奇怪。它清楚地表明您只需要 PIS 对象的 Id 作为 dataContext 。真的是你需要的吗?

    更常规的做法是将完整对象作为 DataContext(如完整 PIS 对象)。

    希望对您有所帮助,否则请尝试更明确地解释您的问题。

    【讨论】:

    • 你说得对,我需要整行的数据上下文,但这并不能解决我的问题,因为我不知道下一步是什么。
    猜你喜欢
    • 2010-10-14
    • 1970-01-01
    • 2011-11-17
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多