【问题标题】:create table with fixed header of with split word in c# windows application在c#windows应用程序中创建带有固定标题的表,带有拆分字
【发布时间】:2013-05-25 16:26:02
【问题描述】:

我想在 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
    }

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

【问题讨论】:

  • 我要制作游戏同款拼图,一个字是秘密,另一个发字去找
  • 请将您当前的代码添加到问题中。让我们知道您在哪里遇到问题。
  • 如果第二个单词比第一个长怎么办?
  • 第二个单词必须等于第一个。我想显示错误。

标签: c# winforms multidimensional-array datagridview


【解决方案1】:

你可以使用类变量:

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);
}

【讨论】:

  • 非常感谢@Alex Filipovici :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多