From: Kali on
Thank you Andy. I really appreciate your replies.

I have a datagridview on the form now with 2 dropdownlist columns defined.
I need to bind the datagridviewcontrol to an existing set of data (few
joined tables) and display the results. Only thing is, I need the first
combobox to contain a list of items from an existing table and have the
value selected already from the original data (by id). Here is the code I'd
use in an asp.net page to bind the grid initially... 2 sql commands, 1 to
fill the combobox and the other to get the existing data and preselect the
value from the combobox. Just cant seem to accomplish this in a windows
form app.

protected void dgBooks_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
//bind drop down list of books
SqlConnection cn = new
SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("cn"));
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Books order by book_title";
cn.Open();

DropDownList ddlBook;
ddlBook = (DropDownList)e.Item.FindControl("ddlBook");
ddlBook.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
ddlBook.DataBind();
ddlBook.Items.Insert(0, "");
cn.Close();
//end of binding drop down list of books

//set values
SqlConnection cn2 = new
SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("cn"));
SqlDataReader dtr;
SqlCommand objCmd;
string strSql;
strSql = "select * from ordered_books where record_id='" +
dgBooks.DataKeys[e.Item.ItemIndex] + "'";
objCmd = new SqlCommand(strSql, cn2);
cn2.Open();
dtr = objCmd.ExecuteReader();
dtr.Read();

ddlBook.Items.FindByValue(dtr["book_id"].ToString()).Selected =
true;

DropDownList ddlQuantityOfBook =
(DropDownList)e.Item.FindControl("ddlQuantityOfBook");
ddlQuantityOfBook.Items.FindByValue(dtr["quantity_of_book"].ToString()).Selected
= true;

cn2.Close();
}
}


"Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
news:%Zeen.108596$5n1.64034(a)newsfe01.ams2...
>
> "Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
> news:lTeen.108591$5n1.94157(a)newsfe01.ams2...
>
> Updating.
> The grid will write data back to the dataset.
> You still need to write the changes back to the database.
>
> Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnUpdate.Click
>
> Update_Data(Me, SqlDa, dsSalesmen)
>
> Fix_Scroll(Me.grdSalesmen)
>
> End Sub
>
> ------------------------
>
>
>
> Try
>
> da.Update(ds)
>
> Catch ex As DBConcurrencyException
>
> MsgBox("There was a problem updating." & vbCrLf & _
>
> "See marked rows" & vbCrLf & _
>
> "This is probably due to someone else working on the data at the same
> time")
>
> Catch ex As SqlException
>
> MsgBox(ex.Errors)
>
> End Try
>
> If ds.HasChanges() Then
>
> ' not everything has successfully updated
>
> ' This is likely due to concurrency issues
>
> Dim dt As DataTable = ds.Tables(0)
>
> Dim dr As DataRow
>
> For Each dr In dt.Rows
>
> If dr.RowState = DataRowState.Added Then
>
> dr.RowError = "Failed to add this row"
>
> ElseIf dr.RowState = DataRowState.Modified Then
>
> dr.RowError = "Failed to update this row"
>
> ElseIf dr.RowState = DataRowState.Deleted Then
>
> dr.RowError = "Failed to delete this row"
>
> dr.RejectChanges()
>
> End If
>
> Next dr
>
> GoTo reset_cursor
>
> Else
>
> ds.Clear()
>
> da.Fill(ds)
>
> MsgBox("Updated")
>
> End If
>
From: Andy O'Neill on

"Kali" <noemail(a)address.com> wrote in message
news:%237xpiOmrKHA.3792(a)TK2MSFTNGP05.phx.gbl...
> Thank you Andy. I really appreciate your replies.
>
> I have a datagridview on the form now with 2 dropdownlist columns defined.
> I need to bind the datagridviewcontrol to an existing set of data (few
> joined tables) and display the results. Only thing is, I need the first
> combobox to contain a list of items from an existing table and have the
> value selected already from the original data (by id). Here is the code
> I'd use in an asp.net page to bind the grid initially... 2 sql commands,
> 1 to fill the combobox and the other to get the existing data and
> preselect the value from the combobox. Just cant seem to accomplish this
> in a windows form app.

I don't follow that code or your problem.
If you bind a column to the id of the selected book then it'll be the one
shown when you present either an asp.net datagridview or a windows
datagridview.
So I don't get why you need some of that code.

Does the list of data in each combo vary depending on a value in another
column?

From: Kali on
No, the list remains the same, just that the selected value would be
different based on what the user originally entered.

Do you have a basic example of a datagridview with 1 combobox column? The
combobox column would contain a list of books as a result of a simple query
(select book_id, book_title from books)...

Books table...
Book_id, Book_Title
1, Whatever book
2, This is a book
3, Another book
4, Some book

There is an Order_Books table that looks like this...

Customer_Number, Book_ID
45, 2
45, 4

The datagridview would be bound by running the quesy "select * from
order_books where customer_number=45"

The grid then gets bound with 2 rows and the comboboxes are auto selected
at...

This is a book
Some book


The user can then add, change or delete a row.






"Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
news:R%fen.2$d03.0(a)newsfe28.ams2...
>
> "Kali" <noemail(a)address.com> wrote in message
> news:%237xpiOmrKHA.3792(a)TK2MSFTNGP05.phx.gbl...
>> Thank you Andy. I really appreciate your replies.
>>
>> I have a datagridview on the form now with 2 dropdownlist columns
>> defined. I need to bind the datagridviewcontrol to an existing set of
>> data (few joined tables) and display the results. Only thing is, I need
>> the first combobox to contain a list of items from an existing table and
>> have the value selected already from the original data (by id). Here is
>> the code I'd use in an asp.net page to bind the grid initially... 2 sql
>> commands, 1 to fill the combobox and the other to get the existing data
>> and preselect the value from the combobox. Just cant seem to accomplish
>> this in a windows form app.
>
> I don't follow that code or your problem.
> If you bind a column to the id of the selected book then it'll be the one
> shown when you present either an asp.net datagridview or a windows
> datagridview.
> So I don't get why you need some of that code.
>
> Does the list of data in each combo vary depending on a value in another
> column?

From: Andy O'Neill on
"Kali" <noemail(a)address.com> wrote in message
news:OGcZjtmrKHA.3800(a)TK2MSFTNGP06.phx.gbl...
> No, the list remains the same, just that the selected value would be
> different based on what the user originally entered.
>
> Do you have a basic example of a datagridview with 1 combobox column? The

That's the simplest bit of code I could find mate.
It does what you want.

> combobox column would contain a list of books as a result of a simple
> query (select book_id, book_title from books)...

So that query goes in your data template.
Setting the id as the datavalue sets the selected item.
( As it would have with your asp.net screen, I don't get why you have all
that findcontrol stuff ).

>
> Books table...
> Book_id, Book_Title
> 1, Whatever book
> 2, This is a book
> 3, Another book
> 4, Some book
>
> There is an Order_Books table that looks like this...
>
> Customer_Number, Book_ID
> 45, 2
> 45, 4
>
> The datagridview would be bound by running the quesy "select * from
> order_books where customer_number=45"

So you put that in the select for the dataadapter or however that works with
the new binder syntax.
Obviously that there customer number will be a parameter but you can have it
hard coded whilst developing.
On the read data button click you can substitute the entire select statement
if you like.

>
> The grid then gets bound with 2 rows and the comboboxes are auto selected
> at...
>
> This is a book
> Some book
>
>
> The user can then add, change or delete a row.

That's how that salesmen screen works..
The only odd thing is that you have 2 tables.
If you can't work out how to generate the dataadapter then do something like
this.
Sorry if I make a istake here or it's different, I haven't written a windows
form since 2004.

Create a table in your database with the same columns as your select query.
Drag and drop and create a strongly typed dataset based on it.
Plus a dataadapter for the table.
You have select, insert, update and delete sql statements for your
dataadapter.
Change the select to the string you have.
Plus the other statements...
Drop the table.

From: Kali on
Thanks Andy. I see where you're defining the columns and adding the items,
etc. but I don't see where you're selecting existing records and allowing
the user to edit. Am I looking at this the wrong way?

"Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
news:yGgen.273$rL6.22(a)newsfe02.ams2...
> "Kali" <noemail(a)address.com> wrote in message
> news:OGcZjtmrKHA.3800(a)TK2MSFTNGP06.phx.gbl...
>> No, the list remains the same, just that the selected value would be
>> different based on what the user originally entered.
>>
>> Do you have a basic example of a datagridview with 1 combobox column?
>> The
>
> That's the simplest bit of code I could find mate.
> It does what you want.
>
>> combobox column would contain a list of books as a result of a simple
>> query (select book_id, book_title from books)...
>
> So that query goes in your data template.
> Setting the id as the datavalue sets the selected item.
> ( As it would have with your asp.net screen, I don't get why you have all
> that findcontrol stuff ).
>
>>
>> Books table...
>> Book_id, Book_Title
>> 1, Whatever book
>> 2, This is a book
>> 3, Another book
>> 4, Some book
>>
>> There is an Order_Books table that looks like this...
>>
>> Customer_Number, Book_ID
>> 45, 2
>> 45, 4
>>
>> The datagridview would be bound by running the quesy "select * from
>> order_books where customer_number=45"
>
> So you put that in the select for the dataadapter or however that works
> with the new binder syntax.
> Obviously that there customer number will be a parameter but you can have
> it hard coded whilst developing.
> On the read data button click you can substitute the entire select
> statement if you like.
>
>>
>> The grid then gets bound with 2 rows and the comboboxes are auto selected
>> at...
>>
>> This is a book
>> Some book
>>
>>
>> The user can then add, change or delete a row.
>
> That's how that salesmen screen works..
> The only odd thing is that you have 2 tables.
> If you can't work out how to generate the dataadapter then do something
> like this.
> Sorry if I make a istake here or it's different, I haven't written a
> windows form since 2004.
>
> Create a table in your database with the same columns as your select
> query.
> Drag and drop and create a strongly typed dataset based on it.
> Plus a dataadapter for the table.
> You have select, insert, update and delete sql statements for your
> dataadapter.
> Change the select to the string you have.
> Plus the other statements...
> Drop the table.