DataGridView displays column names out of order; How do I keep my "ID" column displaying as...

  • Thread starter Thread starter Nubedubedu
  • Start date Start date
N

Nubedubedu

Guest
The code shown below displays column names in a DataGridView. Clicking button1 and button2 changes the "ID" column from being included in the display to being excluded from the display. The number of columns displayed is also changed.

With "//DataTable table = new DataTable();" commented out in button1 and button2 event handlers, the DataGridView displays the "ID" column first when button1 is clicked and then it is not displayed when button2 is clicked ". It then is displayed as the first column when button one is clicked again. This is what I want to happen.

With "DataTable table = new DataTable();" not commented out in button1 and button2 event handlers, the DataGridView displays the "ID" as the first column when button1 is clicked and then it is not displayed when button2 is clicked ". But it is displayed as the last column when button1 is clicked again. It remains the last column no matter how many times button1 and button2 are clicked. The textbox shows that the index of the "ID" column in the Table used as the data source remains zero whenever button1 is clicked and -1 when button2 is clicked.

Why is the display behaving differently in these two cases? I want to write code with the DataTable declared inside my methods like the second case. See the code below: (copy the code into windows Form1 to see the problem)

public Form1()
{
InitializeComponent();

}
DataTable table = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
//DataTable table = new DataTable();
dataSet1.Reset();
table.Reset();
dataSet1.Tables.Add(table);
setTableForAccess(6, "Table1", dataSet1);
bindingSource1.DataSource = dataSet1.Tables[dataSet1.Tables.IndexOf(table)];
dataGridView1.DataSource = bindingSource1;
textBox1.Text = dataSet1.Tables[dataSet1.Tables.IndexOf("Table1")].Columns.IndexOf("ID").ToString();
}
private void button2_Click(object sender, EventArgs e)
{
//DataTable table = new DataTable();
dataSet1.Reset();
table.Reset();
dataSet1.Tables.Add(table);
setTableForAccess1(14, "Table1", dataSet1);

bindingSource1.DataSource = dataSet1.Tables[dataSet1.Tables.IndexOf(table)];
dataGridView1.DataSource = bindingSource1;
textBox1.Text = dataSet1.Tables[dataSet1.Tables.IndexOf("Table1")].Columns.IndexOf("ID").ToString();
}

public void setTableForAccess(int rowCount, string tableName, DataSet dataSet1)//use to setup Access sourced DataSet//1/28/2016
{
rowCount -= 2; rowCount /= 2;//rowcount adjustment for Access data base
for (int j = 0; j < rowCount; j++)
{
if (!dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("ID") || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("SentenceID") || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("Kanji" + j.ToString()) || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("Hiragana" + j.ToString()))
{
if (!dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("ID") || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("SentenceID"))
{
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Clear(); dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Rows.Clear();
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("ID", typeof(int)));
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("sentenceID", typeof(ulong)));
}
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("Kanji" + j.ToString(), typeof(string)));
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("Hiragana" + j.ToString(), typeof(string)));
}
}
}
public void setTableForAccess1(int rowCount, string tableName, DataSet dataSet1)//use to setup Access sourced DataSet//1/28/2016
{
rowCount -= 1; rowCount /= 2;//rowcount adjustment for Access data base
for (int j = 0; j < rowCount; j++)
{
if (!dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("SentenceID") || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("Kanji" + j.ToString()) || !dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("Hiragana" + j.ToString()))
{
if (!dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Contains("SentenceID"))
{
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Clear(); dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Rows.Clear();
//dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("ID", typeof(int)));
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("sentenceID", typeof(ulong)));
}
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("Kanji" + j.ToString(), typeof(string)));
dataSet1.Tables[dataSet1.Tables.IndexOf(tableName)].Columns.Add(new DataColumn("Hiragana" + j.ToString(), typeof(string)));
}
}
}
private void button3_Click(object sender, EventArgs e)
{
dataSet1.Reset();
}

Continue reading...
 
Back
Top