0

我想在 windows 窗体 c# 中创建表。我得到这个词,拆分它,我想将它的字符数组设置为表的标题。当我添加新词时,我想拆分它,将它设置在标题下的新行中,并将每个字符与标题的相同列进行比较。我不知道在表格中输入的字数和字数,我该如何创建它?例子:

word for header: book --> split it and set in header
new word :       bird --> split it and set in new row 

table:
b o o k
b i r d

另一个问题:如果我的窗体被关闭并再次打开,我想再次给出这些话,我不想失去主题。感谢您的帮助...

更新:

  private void wordbtn_Click(object sender, EventArgs e)
    {
    char[] list = wordBox.Text.Replace(" ", "").ToCharArray();
    var repeatedChars = wordBox.Text.Replace(" ", "").ToCharArray().GroupBy(x => x)
        .Where(y => y.Count() > 1).Select(g => new { Letter = g.First(), Count = g.Count() });
    int repeatCount = repeatedChars.Count();
    int listCount = list.Count();
    int cell = repeatCount + listCount + 2;

    List<char[]> ll = new List<char[]>();
    ll.Add(list);

   // DataTable dt = new DataTable();
    //i want to create table for words by characters
    }

但是当我发送新单词时,列表中只有它,其他单词被清除。

4

1 回答 1

0

您可以使用类变量:

DataTable dt;

在您的wordbtn_Click method中,您可以在现有代码之后添加以下代码:

if (dt == null)
{
    dt = new DataTable();
    for (int i = 0; i < list.Length; i++)
    {
        var dc = dt.Columns.Add(String.Format("column_{0}", i));
    }

    dataGridView1.DataSource = dt;

    for (int i = 0; i < list.Length; i++)
    {
        dataGridView1.Columns[i].HeaderText = list[i].ToString();
        dataGridView1.Columns[i].DefaultCellStyle.Padding = Padding.Empty;
    }
}
else
{
    if (list.Length != dt.Columns.Count)
    {
        MessageBox.Show("Word length does not match.");
        return;
    }
    var dr = dt.NewRow();
    for (int i = 0; i < list.Length; i++)
    {
        dr[i] = list[i];
    }
    dt.Rows.Add(dr);
}
于 2013-05-30T11:17:41.407 回答