1
我想編程一個Silverlight應用程序(使用Silverlight 5.1,.Net 4.5),我想要有3個數據網格。從webservices填充datagrid
第一個數據網格顯示症狀列表,第二個數據網格應列出datagrid1中所選症狀的子症狀,第三個數據網格應顯示一種「鹽」來治療子代症狀。數據網格應通過WCF Web服務接收他們的數據。第一個數據網格從表格症狀中獲取所有數據,第二個數據網格僅從webservice獲取datagrid1中所選症狀的子節奏,而「salt」數據網格只應接收來自web服務的選定症狀和子節奏的salt。
只要我選擇了一種症狀,就會出現subsymtpoms,只要我選擇了症狀,就會出現相應的鹽。一切都很好。
我遇到的問題:當我選擇一個subsymptom並且再次選擇Datagrid1中的一個症狀時,所有的datagrids消失。
下面是代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using Rebat.SymptomeService;
namespace Rebat
{
public partial class MainPage : UserControl
{
ServiceClient client = new ServiceClient();
public MainPage()
{
InitializeComponent();
ServiceClient client = new ServiceClient();
client.SymptomeListCompleted += new EventHandler<SymptomeListCompletedEventArgs>(client_SymptomeListCompleted);
client.SymptomeListAsync();
}
void client_SymptomeListCompleted(object sender, SymptomeListCompletedEventArgs e)
{
SymptomeGrid.ItemsSource = e.Result;
}
void client_CustomerListCompleted(object sender, Symptome2ListCompletedEventArgs e)
{
Symptome2Grid.ItemsSource = e.Result;
}
void client_SalzListCompleted(object sender, SalzListCompletedEventArgs e)
{
SalzGrid.ItemsSource = e.Result;
}
private void SymptomeGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome sympt = SymptomeGrid.SelectedItem as Symptome;
client.Symptome2ListCompleted += new EventHandler<Symptome2ListCompletedEventArgs>(client_CustomerListCompleted);
client.Symptome2ListAsync(sympt.sId.ToString());
}
private void Symptome2Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Symptome2 sympt2 = Symptome2Grid.SelectedItem as Symptome2;
client.SalzListCompleted += new EventHandler<SalzListCompletedEventArgs>(client_SalzListCompleted);
//if i remove the next line, datagrids don't dissapear anymore... so this might be the problem
client.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());
}
}
}
這裏的.xaml代碼
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Rebat.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" Margin="-13,0,-543,-335" RenderTransformOrigin="0.499,0.573">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition Height="516*"/>
<RowDefinition Height="119*"/>
</Grid.RowDefinitions>
<sdk:DataGrid x:Name="SymptomeGrid" Margin="20,10,0,0" HorizontalAlignment="Left" Width="249" Grid.RowSpan="2" SelectionChanged="SymptomeGrid_SelectionChanged" Height="227" VerticalAlignment="Top"/>
<sdk:DataGrid x:Name="Symptome2Grid" HorizontalAlignment="Left" Height="227" Margin="293,10,0,0" VerticalAlignment="Top" Width="244" Grid.RowSpan="2" SelectionChanged="Symptome2Grid_SelectionChanged"/>
<sdk:DataGrid x:Name="SalzGrid" HorizontalAlignment="Left" Height="227" Margin="20,275,0,0" Grid.Row="1" VerticalAlignment="Top" Width="401"/>
</Grid>
</UserControl>
,這裏的web服務代碼:
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace Rebat.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
public List<Symptome> SymptomeList()
{
var custList = new List<Symptome>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
const string sql = @"SELECT Feld1, Feld2 FROM Symptome";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Symptome
{
sId = dr.GetInt16(0),
symptom = dr.GetString(1)
};
custList.Add(cust);
}
return custList;
}
}
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public List<Symptome2> Symptome2List(string s1)
{
var custList = new List<Symptome2>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
string sql = @"SELECT ID, sy1, sy2, symptom2 FROM Symptome2 where sy1="+s1;
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Symptome2
{
ID = dr.GetInt32(0),
sy1 = dr.GetInt16(1),
sy2 = dr.GetInt16(2),
symptom2 = dr.GetString(3)
};
custList.Add(cust);
}
return custList;
}
}
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public List<Salz> SalzList(string sym1, string sym2)
{
var salzList = new List<Salz>();
using (OleDbConnection conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
{
string sql = @"SELECT sId, salz FROM Salze where sId in (SELECT saId from Rezept where sy1Id= " + sym1 + " and sy2Id=" +sym2 + ")";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
OleDbDataReader dr = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
if (dr != null)
while (dr.Read())
{
var cust = new Salz
{
SID = dr.GetInt32(0),
Name = dr.GetString(1)
};
salzList.Add(cust);
}
return salzList;
}
}
}
}
}
終於幫助類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Symptome
{
public int sId { get; set; }
public string symptom { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Symptome2
{
public int ID { get; set; }
public int sy1 { get; set; }
public int sy2 { get; set; }
public string symptom2 { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Rebat.Web
{
public class Salz
{
public int SID { get; set; }
public string Name { get; set; }
}
}
但什麼是修復?原始代碼有什麼問題? –
在第一個網格的選擇已更改的事件上,您需要清除其他兩個數據網格,對於第二個數據網格您需要清除第三個數據網格。您還必須檢查所選項目是否爲空。否則會有例外。這就是爲什麼網格沒有顯示任何東西! – Sajeetharan
我建議你編輯你的答案只包括相關的變化。不需要包含與原始代碼相同的代碼。 –