0

I using microsoft visual studio, and i have box number in datagridview as follows

Box Number
4433002
4433006
4433011

I want do some sql query like "SELECT NOTE FROM TABLE WHERE BOX_NUMBER IN (datagridview list)"

I have try this

"SELECT NOTE FROM TABLE WHERE BOX_NUMBER IN '"+ dataGridView4.SelectAll +"'";

but still eror

1
  • Where do you need the results to appear? More information of what you're trying to do is needed to be able to assist here. If you just want to be able to retrieve the data for later on, try creating a DataTable which is populated with the Box_Number values, and use that DataTable to populate your grid for the visual results. You can then use that DataTable elsewhere and apply a query/filter to that rather than using the gridview.
    – Tom Camish
    Commented Jun 7 at 9:06

1 Answer 1

0

The SelectAll method does something else. This method selects all cells. You should read the values ​​with a code similar to the code below It should be noted that parentheses are also required in your SQL query, which I added in the GetBoxNumbersAsString method.

    private string GetBoxNumbersAsString(bool IncludeParentheses = true)
    {
        var BoxNumbers = GetListOfCellValues(0);
        var BoxNumbersAsString = string.Join(", ", BoxNumbers);
        if (IncludeParentheses)
            BoxNumbersAsString = "(" + BoxNumbersAsString + ")";
        return BoxNumbersAsString;
    }
    private List<int> GetListOfCellValues(int ColumnIndex = 0)
    {
        var ListOfCellValues = new List<int>();
        foreach (DataGridViewRow Row in dataGridView1.Rows)
        {
            var Value = Row.Cells[ColumnIndex].Value;
            if (Value != null) 
                // The last row may be empty in edit mode!
                ListOfCellValues.Add(int.Parse(Value.ToString()));
        }
        return ListOfCellValues;
    }

Your SQL query should look like the following:

"SELECT NOTE FROM TABLE WHERE BOX_NUMBER IN " + GetBoxNumbersAsString(true)

Not the answer you're looking for? Browse other questions tagged or ask your own question.