我正在尝试创建一个类似于MS Access使用的查询生成器。我有一个绑定到DataGridView的数据。当用户激活表单时,他或她可以在DataGridView中输入值,然后将其反映在数据表中。我想从数据表中检索这些值,但是有一个问题:
每一行都会创建自己的' AND'语句,当它移动到一个新行时,它会添加一个' OR'。例如:
XXX OR RRR | | CCC | | DDD |
-----------|--------|--------------|-------------|-------------- |
| | BBB | | |
因此,下表的用户输入的最终结果应为:
((XXX OR RRR)和CCC AND DDD)或BBB
这与MS Access中查询的设计视图非常相似。
我尝试过使用GetValues()方法,但由于某些原因似乎没有触发。以下是我的代码片段:
// Build string and then run to test and have user scroll through data 'test' 'run' after they view save, dynamic or showing up, character check
private void auto_Click(object sender, EventArgs e)
{
DataTable changed = new DataTable();
changed = dt.GetChanges();
where_Clause = "";
row_Count = auto_GridView1.Rows.Count; // SHould the datagridview or the data rows be counted
column_count = auto_GridView1.Columns.Count;
for (int i = 0; i < row_Count; i++)
{
// where_Clause += (row_Count - 1) ? "AND" + auto_GridView1[i, j].ToString() : string.Empty;
if (i > 0)
{
where_Clause = where_Clause + " OR";
}
for (int j = 0; j < column_count; j++)
{
// Need to see if the Value of the cell changed. If so must validate
where_Clause += (i < column_count - 1) ? "AND" + dt.Rows[i][j].ToString() : string.Empty; // USE DATA TABLE
}
}
这循环遍及整个表格,基本上连接了一大堆&#39; AND&#39;有什么想法吗?
答案 0 :(得分:0)
我明白了。我用一个输入循环遍历每个单元格并将其转换为简单放置的字符串。我打电话给其他职能部门来实现其他目标。
private void auto_Click(object sender, EventArgs e) // Build string and then run to test and have user scroll through data 'test' 'run' after they view save, dynamic or showing up, character check
{
//DataTable changed = new DataTable();
// changed = dt.GetChanges();
where_Clause = "((";
row_Count = auto_GridView1.Rows.Count; // SHould the datagridview or the data rows be counted
column_count = auto_GridView1.Columns.Count;
for (int i = 0; i < row_Count - 1; i++) // Begins by selecting the row
{
char[] MyChar = { 'A', 'N', 'D', ' ' };
where_Clause = where_Clause.TrimEnd(MyChar); // Trims in the loop however doesnt pick up the last AND Still leaves last one
if (i > 0 ) // Adds the 'OR' after Row [0] b/c 'OR' isnt needed at row[0]
{
where_Clause = where_Clause + " ) OR ( ";
}
for (j = 0; j < column_count - 1; j++) // Loops through all the columns in a given row
{
if (dt.Rows[i][j].ToString() != "") // Checks to see if there is a value in the box. If so it adds it to the Clause
{
header = "";
string current_cell = "";
header = get_correct_header(j); // Use j to get the header ]
current_cell = dt.Rows[i][j].ToString(); // gets the current selection
current_cell = insert_correctHeader(current_cell, header); // reads throught he string pickling up the necessary symbols and inserting in the correct header name
where_Clause += (i < column_count - 1) ? current_cell + " AND " : string.Empty; // USE DATA TABLE
}
}
}
char[] Mychar2 = { 'A', 'N', 'D', ' ' }; // Trim again to get rid of that final AND
where_Clause = where_Clause.TrimEnd(Mychar2);
where_Clause = where_Clause + "))";
MessageBox.Show(where_Clause); // We have the where clause here must be tested to see if it works