Winform上的TextBox绑定到DataTable中的字符串列 [英] TextBox On Winform Bound to String Column In DataTable
问题描述
我正在使用bindingSource将我的文本框绑定到DataTable。 我的问题是,当我的数据表中的字符串列具有空值时,文本框将显示01/01/0001 00:00:000 如果是无效日期,我希望文本框保持为空。
这是我的代码
I am using bindingSource to bind my text box to a DataTable. My issue is that when my string column in the Data Table has a null value the text box will display 01/01/0001 00:00:000 If it is an invalid Date I want the text box to remain empty. This is my code
private void Form1_Load()
{
Getmeinfo();
Binddata();
}
private static void Binddata()
{
txtOne.DataBindings.Add("Text", bindingSource1, "tsd");
}
private static void Getmeinfo()
{
DataTable dtData = new DataTable();
DataRow newRow = dtData.NewRow();
dtData.Columns.Add("tsd", typeof(string));
string date = Convert.ToString(listItem[TSD_Info"]);
newRow["tsd"] = date;
}
推荐答案
Hi IndigoMontoya,
Hi IndigoMontoya,
>>如果是无效日期,我希望文本框保持为空。
>>If it is an invalid Date I want the text box to remain empty.
你的意思是什么无效?
我测试了你的代码,但我找不到你将数据表绑定到bindingsource的任何地方,试着参考改为使用以下代码:
I tested your code but I could not find any place where you bound the datatable to the bindingsource, try to refer to the following code instead:
private void Binddata()
{
textBox1.DataBindings.Add("Text", dtData, "tsd");
}
static DataTable dtData = new DataTable();
private static void Getmeinfo()
{
DataRow newRow = dtData.NewRow();
dtData.Columns.Add("tsd", typeof(string));
string date = Convert.ToString(listItem["TSD_Info"]);
newRow["tsd"] = date;
dtData.Rows.Add(newRow);//also you need to add the row to datatable
}
问候,
Frankie
这篇关于Winform上的TextBox绑定到DataTable中的字符串列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!