我需要您的幫助,瞭解如何將List綁定到DataGridView。我試圖使用BindingList<T>
,但它仍然沒有在我的gridview中顯示記錄。我嘗試使用List<T>
,但它仍然無法正常工作。綁定列表<class>到DataGridView
下面是我使用的代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ToolClientController ctrl = new ToolClientController();
IpAddressTextbox.Text = ctrl.GetIPv4Config();
PortNumberTextbox.Text = ctrl.GetPortNumber();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
ToolMsgFileTextbox.Text = openFileDialog1.FileName;
}
}
private void UploadButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ToolMsgFileTextbox.Text))
{
FileTypeError.SetError(UploadButton, "Please enter filename.");
return;
}
if (!openFileDialog1.FileName.Contains(".txt"))
{
FileTypeError.SetError(UploadButton, "File should be in .txt");
return;
}
ToolClientController ctrl = new ToolClientController();
List<ToolMessages> test = new List<ToolMessages>();
ToolMessages tool = new ToolMessages();
tool.IsPass = true;
tool.ToolMessageReply = string.Empty;
tool.ToolMessageRequest = "x";
test.Add(tool);
MessageGridViews.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
col1.DataPropertyName = "ToolMessageRequest";
col1.HeaderText = "Request";
MessageGridViews.Columns.Add(col1);
BindingList<ToolMessages> bind = new BindingList<ToolMessages>(test);
MessageGridViews.VirtualMode = true;
MessageGridViews.DataSource = bind;
}
}
我的理解是正確的,你點擊'UploadButton'並且你沒有看到輸出,如我在下面的答案,即列「Request」wih值「x」?另外,如果你根本沒有使用它,那麼你在'UploadButton_Click'中創建'ToolClientController'是什麼?在不同的說明中,您是否嘗試將斷點放入'UploadButton_Click'方法中以實際看到會發生什麼?你的代碼是否得到執行? – 2014-10-08 09:52:35