I am creating a small application using vb.net 2008 and sql server.I want to create a search form .When i click in the SearchToolStripButton
in all forms the 'Search' form will be loaded with the data from the header table into a grid.How to write code for the following

1)When i double click in one row in the grid of search window i want to return those data to the corresponding forms and i want to edit there.
OR
After i select one row in grid in the search form and click OK button i want to return those data to the corresponding forms.

Kindly do the needful.

The easiest way would be to pass from the search form to the edit form a datatable as a parameter when you load it and have the edit form on load populate the appropriate fields from the datatable.

dear adam_k

Can u kindly provide the code to be written in search form & edit form

hello ajkva !
if my main form has a txtUserID , txtUsername , and i want to search them by using an other form , then i have to take a new form and place a grid on it and also take bindingsource component , now code this on the load event of the searching form

dim mycon as new sqlconnection("connection string")
dim ds as new dataset
mycon.open
dim da as new dataadapter("select userid , username from users",mycon) 
ds.table("User").clear
da.fill(ds,"user")
bindingsource1.datasource = ds.table("user")
datagridview1.datasource = bindingsource1
mycon.close()

by using this code , on the load event of the form the datagrid is populated with records , then what u have to do is to code on the keypress event and on the double click event of the grid ,
now first we have to code for the double click event of the grid , but make sure before coding , that selection property of ur grid is set to row selection
now place this code on the double click event of the grid

form1.txtuserid.text = datagridview1.item("userid",datagridview1.currentrow.index)
.value.tostring
form1.txtusername.text =datagridview1.item("username",datagridview1.currentrow.index)
.value.tostring
me.close()

after this when u double click on the grid , ur txtuserid and txtusername is populated , and ur selection form is closed , now move toward the second step , we have to code for the keypress of the grid ,

if e.keychar= chr(keys.enter) then 
form1.txtuserid.text = datagridview1.item("userid",datagridview1.currentrow.index-1)
.value.tostring
form1.txtusername.text =datagridview1.item("username",datagridview1.currentrow.index-1).value.tostring
me.close()
endif

after all this , ur program will work perfectly
hope this will helps u :)

Best Regards

M.Waqas Aslam

Thanks Waqas Aslam

The code was working fine,But my serach should work for all forms instead of 'USER' form.How to write code in that scenario.

hello ajkva !

u have to declare module and then declare some global variables so that only a single form can is used for searching for two form User and Product.for example

public UserForm as boolean 
public ProductForm as boolean

then where ever u want to call the searching form then make the value of the boolean variable true ,and write the following code in the load event of the searching form.\

if UserForm = true then 
userSearching() ' this sub is for user searching
endif
if ProductForm = true then 
ProductSearching()' this sub is for Product searching
endif

and then put all the previous coding according to ur requirement .

Hope this will helps u
Regards
M.Waqas Aslam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.