wpfdatagrid資料庫
1. C# WPF中DataGrid調用資料庫的時候,發生下一條數據覆蓋上一條數據,如下圖兩條if語句
你是直接 DataGrid.ItemsSource = datatable.DefaultView;
這樣就是每次指定一個數據源, 他肯定是覆蓋掉原有記錄的。
就好比
int a;
if(xxx)
a =1 ;
if(yyy)
a =2 ;
他肯定不會輸出3的。
如果需要累加的顯示所有歷史記錄,有兩種辦法,
1、使用ObservableCollection 即WPF提供的動態通知類集合。
如
先指定DataGrid.ItemsSource = Data;
private ObservableCollection<Model.MyData> _data= null;
public IList<Model.MyData> Data
{
get { return _data?? (_data= new ObservableCollection<Model.MyData>()); }
}
每次需要查詢資料庫的時候Data.Add(新紀錄) ; 不要去重復進行數據源綁定。
2、資料庫查詢時累加,
DataTable dt = new DataTable();
if(xxx)
//把查詢的結果加入dt
dt.Rows.Add( newrow);
if(yyy)
dt.Rows.Add(newrow);
每次添加完 要重新綁定:DataGrid.ItemsSource = dt.DefaultView;
第二種方法效率比較低。
2. wpf datagrid資料庫讀取的列名如何改成中文
在DataGridView設置數據源綁定後,
設置DataGridView的屬性HeaderText就可以了。
代碼參考:
dataGridView.Columns[filedName].HeaderText
=
displayLabel
別的地方找的答案,不知道問的是不是這個,呵呵,另外雙手奉上阿里雲伺服器券:網頁鏈接
3. wpf中如何將datagrid控制項綁定到資料庫上呀
xaml中綁定:
<DataGrid Name="dataGrid1" >
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date}"/>//table表中的欄位
</DataGrid>
xaml.cs:
string con = "Provider=System.Data.sqlClient;Data Source=.;database=logistics;User ID=sa;Password=sa123";
SqlConnection sCon = new SqlConnection(con);
sCon .Open();
string sql = "select * from table";
DataTable dt = new DataTable();
SqlDataAdapter da=new SqDataAdapter(sql,sCon );
DataSet ds=new DataSet();
ds.Fill(ds,"dtName");
dataGrid1.ItemsSource = ds.Tables["dtName"].DefaultView;
4. WPF 如何將.dat數據流讀入到程序datagird里,或寫入資料庫里
您好,這樣:
private void dg1_Loaded(object sender, RoutedEventArgs e)
{
dg1.ItemsSource = SqlHelper2.ExecuteDataTable("select xm,mm,bm from z_qx").AsDataView();
}
可是有一個問題,在datagrid控制項中顯示的標題是資料庫中的英文欄位名稱,
我想實現將標題改成中文的,並綁定相應額列,改如何實現
<DataGrid AutoGenerateColumns="False" x:Name="dg1" Loaded="dg1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" ??? ></DataGridTextColumn>
<DataGridTextColumn Header="密碼" ???>DataGridTextColumn>
<DataGridTextColumn Header="部門" ???></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
===================================================
<DataGrid AutoGenerateColumns="False" x:Name="dg1" Loaded="dg1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Header="姓名" Binding="{Binding Path=name}" />
<DataGridTextColumn Header="密碼" Binding="{Binding Path=pass}"/>
<DataGridTextColumn Header="部門" Binding="{Binding Path=dept}"/>
</DataGrid.Columns>
</DataGrid>
5. WPF中使用datagrid顯示資料庫裡面的內容
這是一個小技巧啦。在Grid的最後一列,比如你圖上的「password」列,將這個列的寬度不設定為固長,而是設為自適應寬度(*)就可以了。
6. WPF的DataGrid自動綁定資料庫時最後的空列到底要怎麼消除
將AutoGenerateColumns設置為false。然後在代碼中手動生成不同表相對應的百分比列寬的與資料庫表的列名對應的列。其餘部分不變即可。
代碼:
string attr = "";
dataDataGrid.AutoGenerateColumns = false;
MultiSecurity ms = new MultiSecurity();
OracleConnection oraCon = ms.CreateConnection("msdb", "maple", "manager");
foreach (string att in ms.getAttributes(tablename, oraCon)) //獲取指定表的所有列
{
attr += att + ",";
dataDataGrid.Columns.Add(new DataGridTextColumn() { Header = att, Binding = new Binding(att), Width = new DataGridLength(2, DataGridLengthUnitType.Star) }); //為DataGrid生成百分比列寬的列,相當於xaml中設置Width = "2*"
}
attr = attr.Substring(0, attr.Length - 1);
sql = "select "+attr+" from " + node.Name; //生成查詢指定列的sql語句
DataTable dt = ms.getData(sql, oraCon);
dataDataGrid.ItemsSource = dt.DefaultView; //將結果集綁定到DataGrid
7. WPF,datagridview顯示資料庫中的內容,為何總顯示下面的錯誤呀,DataGrid哪裡不對了呀~求高手指點,謝謝
DataGrid是控制項類型,不是控制項實體對象,所以會報錯。
如果你在前台初始化了一個DataGrid。那麼你在前台可以給這個DataGrid命名。
<DataGrid x:Name="Mydatagrid" ...>
後台就可以這么寫 Mydatagrid.ItemsSource =...
如果你在後台初始化的DataGrid
如:DataGrid datagrid = new DataGrid ();
那麼就可以這么設置屬性:datagrid .ItemsSource =...
8. WPF中 Datagrid 綁定到資料庫後如何對用戶輸入的數據進行驗證
使用ValidationRule。以下為示例:
Binding testBinding = new Binding();
testBinding.ValidationRules.Add(new IPv4ValidationRule());//添加驗證,根據數據類型添加不一樣的驗證類
testBinding.Mode = BindingMode.TwoWay;
testBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
testBinding.Path = new PropertyPath("[" + myTextBox.Name + "].Permit");//綁定源路徑
testBinding.Source = dic; //綁定源,
myTextBox.SetBinding(TextBox.TextProperty ,testBinding );//綁定目標屬性
數據驗證也是加的數據驗證類繼承ValidationRule。
public class IPv4ValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var str = value as string;
if (String.IsNullOrEmpty(str))
{
MessageBox.Show("請輸入日期。");
//return new ValidationResult(false,
// "Please enter an IP Address.");
}
return new ValidationResult(true, null);
}
}
9. wpf中如何將datagrid控制項綁定到資料庫
給你找了個例子,是直接綁定到資料庫里的table的
後台代碼:
MKP.MyCommon.SQLHelper
sh
=
new
MKP.MyCommon.SQLHelper(null);
DataTable
dt=sh.GetDataTable("select
Fi_id,Fs_pCompanyName,Fs_phone,Fs_email
from
TpmCompany");
dataGrid1.ItemsSource
=
dt.DefaultView;
前台代碼:
<pre
class="csharp"
name="code"><DataGrid
AutoGenerateColumns="False"
Height="153"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="449"
SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns
>
<DataGridTextColumn
Header="ID"
Binding="{Binding
Path=Fi_id}"/>
<DataGridTextColumn
Header="公司名稱"
Binding="{Binding
Path=Fs_pCompanyName}"/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_phone}"
Header="電話"
/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_email}"
Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
10. wpf datagrid 從資料庫中刪除選中復選框的記錄
當數據定時,將id也綁定上,但不顯示,當勾選復選框時,獲取該行數據的id,然後根據id去資料庫中刪除數據