'VB.NET DataGridView when I do the search, I would like to exclude the rows that do not have the results I searched for, how to do?
When I search for a number with my textbox, in the datagridview I find all the results together with the lines that do not have the search result. I would like the datagridview to return me only the rows with the results excluding those not necessary for the search. How to do?
Public Sub FilterData(ValueToSearch As String)
Try
Dim SearchQyery As String = "SELECT * FROM LottoDeaBendata WHERE CONCAT_WS([Id],[Data],[Ruota],[Estratto1],[Estratto2],[Estratto3],[Estratto4],[Estratto5])LIKE'%" & ValueToSearch & "%'"
Dim command As New SqlCommand(SearchQyery, connection)
connection.Open()
Dim table As New DataTable()
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
DataGridView1.DataSource = table
connection.Close()
Catch ex As Exception
MsgBox(ex.Message) 'show error msg'
End Try
End Sub
Private Sub btnFiltraDati_Click(sender As Object, e As EventArgs) Handles btnFiltraDati.Click
FilterData(txtRefreshFiltra.Text)
End Sub
Solution 1:[1]
Tested to work as expected
Private Sub m_Filter() Handles Button1.Click, Me.Shown
Dim dt As New DataTable
Dim SearchQuery As String = $"SELECT * FROM dbo.cfgEggTypes WHERE CONCAT(cEggType,cSpecies,cOldEggType, cQBItemAbbr) LIKE '%{txtSearch.Text}%'"
Dim SqlConnection As New SqlConnection(ConnString)
Dim command As New SqlCommand(SearchQuery, SqlConnection)
Dim SqlDataAdapter = New SqlDataAdapter(command)
command.CommandType = CommandType.Text
SqlDataAdapter.Fill(dt)
dgvExample.DataSource = dt
End Sub
Make sure your SQL query is working as expected in an SQL editor. Set a break point, and copy the exact query text to your sql query editor, and make sure it is correct. If it is correct, your datagridview isn't being cleared properly. You could add dgvExample.DataSource = Nothing to the top of the method to ensure it gets cleared.
Solution 2:[2]
I got it working the right way using Zustand context provider and initializing it using Nextjs pageProps and getStaticProps().
_app.js
function MyApp({ Component, pageProps }) {
const createStore = initStore(pageProps.initZustand);
return (
<Provider createStore={createStore}>
<Component {...pageProps} />
</Provider>
);
}
export default MyApp;
store.js
export const { Provider, useStore } = createContext();
let store = {
names: ["Name 1", "Name 2", "Name 3"],
random: [],
setRandom: (data) => {
set((state) => ({ random: data }));
},
};
export const initStore = (data) => {
const createStore = () =>
create(
devtools((set, get) => ({
...store,
...data,
}))
);
return createStore;
};
index.js getStaticProps:
export async function getStaticProps(context) {
const res = await fetch("https://jsonplaceholder.typicode.com/users");
const posts = await res.json();
return {
props: {
initZustand: {
posts: posts,
},
},
};
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ejbiker93ss |
| Solution 2 | Alex Botham |
