0
I have a datagridview that takes the data from the sqlserver database and needed to make queries, but as they are a lot of data we can’t keep pulling from the database. wanted to do searches through a textbox with the data that are already in datagridview. type I type 'c' and everything that has c appears, regardless of which column it is. I am using windows form, bringing the data by layers. caught in the database the data like this:
public class ConsultarSaida
{
public SqlDataAdapter dataAdapter = new SqlDataAdapter();
public DataTable TabelaEntrada = new DataTable();
public void GetData(SaidasDTO dados)
{
// EntradaDTO Informacoes = new EntradaDTO();
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
string sql = "select vi.prestador_id,vi.tipo,vi.img3,pres.nome,pres.cpf,pres.rg from visita vi inner join prestador pres on pres.id = vi.prestador_id where vi.condominio_id = '" + Informacoes.Condominio_id + "' and vi.datahora_saida is null order by vi.id asc";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(sql, Conexao.obterConexao());
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
TabelaEntrada.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(TabelaEntrada);
}
catch (SqlException e)
{
dados.mensagens = "Erro - Cliente " + e.Message;
}
}
}
and in the form thus:
public void ConsultaVisita()
{
SaidasDTO dados = new SaidasDTO();
ConsultarSaida consultarsaidas = new ConsultarSaida();
//Chamar o método para gravar os dados acima
consultarsaidas.GetData(dados);
//consultarclientes.
dgvSaida.Rows.Clear();
foreach (DataRow linha in consultarsaidas.TabelaEntrada.Rows)
{
dgvSaida.Rows.Add(linha.ItemArray);
}
if (dgvSaida.SelectedRows.Count > 0)
{
int index = dgvSaida.SelectedRows[0].Index;
if (index >= 0)
dgvSaida.Rows[index].Selected = false;
}
}
You can filter the lines using the lines.Itemarray , converts the array into a string (string.Join(",", arr);) and then just adds to the lines that the string contains the key that you are looking for.
– Jorge Costa