0
我有兩個表的文本文件就可以了這樣添加表從文本文件C#
1:"Transmitter",1,grid,32,6
2:"Parameters", 2,list,64
我想說明兩個表在我的形式形成,並且如果需要的話,用戶可以添加更多的表到文本文件。如果用戶在窗體中添加更多表格,則附加表格將顯示在窗體中。
我使用的標籤控制的標籤添加到從指定的文件的形式,併爲表我使用數據網格視圖
這裏是我的代碼:
public partial class Form1 : Form
{
String line;
String typeOfTable;
int amountOfRows;
int amountOfColumns;
private const String TABS = "TABS";
private const String TABLES = "TABLES";
private const String GRID = "grid";
private const String LIST = "list";
private const int LOCATION_TABLE_TYPE = 2;
private const int LOCATION_TABLE_AMOUNT_ROWS = 3;
private const int LOCATION_TABLE_AMOUNT_COLUMNS = 4;
private const int AMOUNT_Columns_IN_LIST = 2;
String[] splitTableArray;
public Form1()
{
InitializeComponent();
getFormContentFromFile();
}
private void getFormContentFromFile()
{
using (StreamReader reader = new StreamReader("neo2G.res"))
{
while (!reader.EndOfStream)
{
line = reader.ReadLine();
if (line.Equals(TABS))
{
while (!line.Equals(".."))
{
line = reader.ReadLine();
if (!line.Equals(".."))
{
createTabInForm(line);
}
}
}
if (line.Equals(TABLES))
{
while (!line.Equals(".."))
{
line = reader.ReadLine();
if (!line.Equals(".."))
{
splitTableLineToArray(line);
typeOfTable = splitTableArray[LOCATION_TABLE_TYPE];
amountOfRows = int.Parse(splitTableArray[LOCATION_TABLE_AMOUNT_ROWS]);
if(typeOfTable.Equals(GRID))
{
amountOfColumns = int.Parse(splitTableArray[LOCATION_TABLE_AMOUNT_COLUMNS]);
dataGridView1.DataSource = createGridForForm(amountOfRows, amountOfColumns);
}
else if(typeOfTable.Equals(LIST))
{
dataGridView1.DataSource = createListForForm(amountOfRows);
}
}
}
}
}
}
}
private void createTabInForm(String tabName)
{
tabName = Regex.Replace(tabName, @"[\d-]", string.Empty);
tabName = tabName.Trim(':', '"');
TabPage myTabPage = new TabPage(tabName);
tabControl1.TabPages.Add(myTabPage);
}
private void splitTableLineToArray(String tableLine)
{
splitTableArray = tableLine.Split(',');
}
public DataTable createGridForForm(int rows, int columns)
{
// Create the output table.
DataTable table = new DataTable();
for (int i = 1; i <= columns; i++)
{
table.Columns.Add("column " + i.ToString());
}
for (int i = 1; i < rows; i++)
{
DataRow dr = table.NewRow();
// populate data row with values here
table.Rows.Add(dr);
}
return table;
}
/// <summary>
/// This method builds a DataTable of the data.
/// </summary>
public DataTable createListForForm(int rows)
{
// Create the output table.
DataTable table = new DataTable();
for (int i = 1; i <= AMOUNT_Columns_IN_LIST ; i++)
{
table.Columns.Add("My column " + i.ToString());
}
for (int i = 1; i < rows; i++)
{
DataRow dr = table.NewRow();
// populate data row with values here
table.Rows.Add(dr);
}
return table;
}
}
}
那麼,這是什麼問題?什麼是問題? – DrKoch
如何從我的文本文件中以編程方式添加表格 – Aleksander
好了,您解釋了要執行的操作,您展示的代碼幾乎完成了所解釋的任務。那麼究竟有什麼不工作?你有錯誤嗎?你的代碼出錯了? – DrKoch