I've a DataGridView and a TextBox. I need filter the values from DB in SQL in DataGridView with the TextBox.
Something like this:
This is my code:
public partial class Form1 : Form
{
private DataSet dataSet;
private SqlDataAdapter adapter;
private BindingSource bindingSource = new BindingSource();
private DataView dataView = new DataView();
public Form1()
{
InitializeComponent();
}
private void GetData(string valor)
{
try
{
// Initialize the DataSet.
dataSet = new DataSet();
dataSet.Tables.Add(new DataTable());
dataSet.Tables[0].Columns.Add("numberAsString", typeof(string));
dataSet.Locale = CultureInfo.InvariantCulture;
// Create the connection string for the AdventureWorks sample database.
string connectionString = "connection ";
// Create the command strings for querying the Contact table.
string contactSelectCommand = "SELECT Titulo as Título FROM V_CuetaWeb WHERE Titulo LIKE ('" + valor + "%') GROUP BY titulo ORDER BY titulo DESC";
// Create the contacts data adapter.
adapter = new SqlDataAdapter(
contactSelectCommand,
connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on the contacts select command. These are used to
// update the database.
SqlCommandBuilder contactsCommandBuilder = new SqlCommandBuilder(adapter);
// Fill the data set with the contact information.
adapter.Fill(dataSet, "V_CuetaWeb");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "Seleccione";
chk.Name = "check";
dtgTitulo.Columns.Add(chk);
dtgTitulo.AllowUserToAddRows = false;
dataSet = new DataSet();
GetData(txtFiltroTitulo.Text);
dtgTitulo.DataSource = bindingSource;
// Create a LinqDataView from a LINQ to DataSet query and bind it
// to the Windows forms control.
EnumerableRowCollection<DataRow> contactQuery = from row in dataSet.Tables["V_CuetaWeb"].AsEnumerable()
orderby row.Field<double>("Título") descending
select row;
dataView = contactQuery.AsDataView();
// Bind the DataGridView to the BindingSource.
bindingSource.DataSource = dataView;
string value = "";
dtgTitulo.DataSource = bindingSource;
bindingSource.Filter = "Título LIKE('" + Convert.ToInt64(value) + "%')";
dtgTitulo.AutoResizeColumns();
}
private void txtFiltroTitulo_TextChanged(object sender, EventArgs e)
{
GetData(txtFiltroTitulo.Text);
}
}
But doesn't work 'cause "título" is a float data in SQL. So, any sugerence?
