Hi,I want to know how canwe select a value from a datagrid and search for that value in another databse.Thanks

When the use clicks on a cell in the DataGridView this causes several events to fire. CellClick or SelectionChanged are most likely what you need.

I've been doing something similar just now.
Here is a code sample of what I did.

private void dgTestData_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1)
        return; // column header clicked - do nothing
    // get the row just clicked
    DataRowView drv = dgTestData.Rows[e.RowIndex].DataBoundItem as DataRowView;
    if (drv != null)
    {
        // convert row to strongly typed row from dataset
        TestData.TestsRow row = drv.Row as TestData.TestsRow;
        if (row != null)
        {
            // Do something with row
        }
    }
}

can you please explain this code and what is TestData.Thanks

TestData is my DataSet that has a table called Tests.

If the DataSet is constructed using the VisualStudio designer then this adds a class for the table row; in my case called TestData.TestsRow .

Since my DataGridView was set up by setting the DataSource to an instance of TestData.Tests I want to access the data row using my class.

The DataBoundItem property returns a reference to a DataRowView item (a class used by DataViewGrid to encapsulate the data source type as this could equally be a DataTable or an array).

The DataRowView.Row property references the original row data source; in my case a TestData.TestsRow object.

The as in TestData.TestsRow row = drv.Row as TestData.TestsRow; is a kind of casting. It returns a null if the value is null or not of the specified type.

Can you please have a look at the following code and see where i am going wrong .Thanks

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataSet CustomersDataSet = new DataSet();
            if (e.RowIndex == -1) return; // column header clicked - do nothing    // get the row just clicked    
            DataRowView drv = dataGridView2.Rows[e.RowIndex].DataBoundItem as DataRowView;    
            if (drv != null)    
            {        //convert row to strongly typed row from dataset        
                CustomersDataSet.TestsRow row = drv.Row as CustomersDataSet.TestsRow;    
                DataSet dt = new DataSet();
                if (row != null)
                {
                    SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
                   
                    SqlDataAdapter da = new SqlDataAdapter();
                    SqlCommandBuilder cmdBuilder;
                    string myString;
                    DataSet ds = new DataSet();
                    //cn.ConnectionString = "Server=server;Database=RTLUser;UID=sa;";
                    cn.Open();
                    myString = @"INSERT INTO wordsTbl(word)  Values('" + drv +"')";
                    SqlCommand myCmd = new SqlCommand(myString, cn);
                    myCmd.ExecuteNonQuery();

                    cn.Close();           
                
                //}    
            }

What is the problem?
[Eduit] Oh, I see.
You need to define the DataSet class and set the DataGridView to an instance of this class.

How are you putting values in to the datagrid?

The problem is that i am using datatable not dataset .Secod when i get the result in databse its not actually the value its is eneterd as 'System.Data.DataRowView'.Thanks for all the help

Looking at your earlier post re DataGrid it is clear that you are not using a strongly typed DataSet as I did.
Here is the CellClick event (not CellContentClick) using standard data classes.

private void dgTestData_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex == -1)
        return; // column header clicked - do nothing

    // get the row just clicked
    DataRowView drv = dgTestData.Rows[e.RowIndex].DataBoundItem as DataRowView;
    if (drv != null)
    {
        // convert row to strongly typed row from dataset
        DataRow row = drv.Row as DataRow    //<-- change to use DataRow
        if (row != null)
        {
            // Do something with row
            // e.g.  int someValue = (int)row["IndexCol"];
        }
    }
}

In your case the Do something part will include : myString = @"INSERT INTO wordsTbl(word) Values('" + (string)row["word"] +"')"; as well as the rest of your code to execute the sql command.

Thanks a lot .Is there any tutorial or book i can refer too for this.As you can see i am fairly new to this.

and one more last question for you.If i want to include a button for the same purpose how can i refer to the datagrid in the code of button.So when u click submit button whatever row is selected in the datagrid should be added to the databse.Thanks a ton for all the help

First set DataGridView.MultiSelect to false in Form_Load to only allow single row selection.
Then you can use dataGridView2.SelectedRows. E.g

if (dataGridView2.SelectedRows.Count > 0)
{
    DataRowView drv = dataGridView2.SelectedRows[0].DataBoundItem as DataRowView;
    // etc...
}
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.