【发布时间】: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