From: Andy O'Neill on

"Kali" <noemail(a)address.com> wrote in message
news:OIjyrmcrKHA.4492(a)TK2MSFTNGP05.phx.gbl...
> Thanks for the reply. I'm just finding it extremely difficult to create
> the same datagrid on a windows form as I do on an asp.net page. On an
> asp.net page I'm able to explicitly define the columns and controls within
> the column, etc. but not so in a windows form. When editing the asp.net
> page datagrid (via click of an edit button) I can set the control types
> and bind them based on existing record in sql table, etc, but not so in a
> windows form. Can you point me to a good reference for accomplishing
> these tasks?

It's different, but easy.

You don't need buttons,
It works like Access.
To edit an editable windows datagrid row you just click on the row.
You get nice edited icons to the left of each row and all sorts of stuff
dead easy.
Insertion?
Click on that star row at the end of your grid.

Tou can specify what's going on with the columns in code or there's a
columns collection
if you look under properties.

I'm guessing you're binding to something that's read only.
If you bind to a dataset/table you just get a load of functionality that
you have to work for on an asp.net datagrid.
This is VB but will give you the idea.
http://www.code-magazine.com/Article.aspx?quickid=0301071
Google "Introduction windows datagrid" for a shed load more topics.
( Do that first next time mate ).

All that retaining state and reducing load on the web server stuff largely
goes out the window.
So (small) datasets are a much more acceptable solution in windows than web
IMO.
Editing a dataset in a web app would be crazy because each time you submit
there's like 3 versions of all your data flying down the wire.
Then it's held on the web server.
So you use datareaders or whatever.
With windows the client reads data into a dataset, the 3 copies are held on
the client and only the changed data needs to be written back.
So strongly datasets are a dead easy solution and also a pretty good one.

The odd thing to throw to the back of your mind is that there's implicitly a
dataview which dotnet creates and sits between your table and the grid.
For some manipulation you need to work with that.
Or I did back in 2004 anyhow.

Hope this helps.

From: Kali on
Thank you, Andy. Will take a look at the article. Some of the issues I'm
having are the event handlers compared to asp.net... onitemdatabound, etc.
I am able to add a combobox to a datagridview and populate its listitems,
however, I'm not able to keep that combobox when binding the data from an
existing set of data.

"Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
news:7R8en.160473$8K4.112369(a)newsfe15.ams2...
>
> "Kali" <noemail(a)address.com> wrote in message
> news:OIjyrmcrKHA.4492(a)TK2MSFTNGP05.phx.gbl...
>> Thanks for the reply. I'm just finding it extremely difficult to create
>> the same datagrid on a windows form as I do on an asp.net page. On an
>> asp.net page I'm able to explicitly define the columns and controls
>> within the column, etc. but not so in a windows form. When editing the
>> asp.net page datagrid (via click of an edit button) I can set the control
>> types and bind them based on existing record in sql table, etc, but not
>> so in a windows form. Can you point me to a good reference for
>> accomplishing these tasks?
>
> It's different, but easy.
>
> You don't need buttons,
> It works like Access.
> To edit an editable windows datagrid row you just click on the row.
> You get nice edited icons to the left of each row and all sorts of stuff
> dead easy.
> Insertion?
> Click on that star row at the end of your grid.
>
> Tou can specify what's going on with the columns in code or there's a
> columns collection
> if you look under properties.
>
> I'm guessing you're binding to something that's read only.
> If you bind to a dataset/table you just get a load of functionality that
> you have to work for on an asp.net datagrid.
> This is VB but will give you the idea.
> http://www.code-magazine.com/Article.aspx?quickid=0301071
> Google "Introduction windows datagrid" for a shed load more topics.
> ( Do that first next time mate ).
>
> All that retaining state and reducing load on the web server stuff largely
> goes out the window.
> So (small) datasets are a much more acceptable solution in windows than
> web IMO.
> Editing a dataset in a web app would be crazy because each time you submit
> there's like 3 versions of all your data flying down the wire.
> Then it's held on the web server.
> So you use datareaders or whatever.
> With windows the client reads data into a dataset, the 3 copies are held
> on the client and only the changed data needs to be written back.
> So strongly datasets are a dead easy solution and also a pretty good one.
>
> The odd thing to throw to the back of your mind is that there's implicitly
> a dataview which dotnet creates and sits between your table and the grid.
> For some manipulation you need to work with that.
> Or I did back in 2004 anyhow.
>
> Hope this helps.

From: Kali on
The biggest problem I am having is figuring out where I do the ItemDataBound
on the datagridview. In asp.net I'd have...

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();
}
}

However, I cannot locate an itemdatabound event arg in the datagridview in a
windows form.

Am I missing something basic? I may be going about this wrong.

I'm binding to a table that is both read and write. User can update.

I'd certainly appreciate any feedback you may have. Thank you very much,


"Andy O'Neill" <aon14nocannedmeat(a)lycos.co.uk> wrote in message
news:7R8en.160473$8K4.112369(a)newsfe15.ams2...
>
> "Kali" <noemail(a)address.com> wrote in message
> news:OIjyrmcrKHA.4492(a)TK2MSFTNGP05.phx.gbl...
>> Thanks for the reply. I'm just finding it extremely difficult to create
>> the same datagrid on a windows form as I do on an asp.net page. On an
>> asp.net page I'm able to explicitly define the columns and controls
>> within the column, etc. but not so in a windows form. When editing the
>> asp.net page datagrid (via click of an edit button) I can set the control
>> types and bind them based on existing record in sql table, etc, but not
>> so in a windows form. Can you point me to a good reference for
>> accomplishing these tasks?
>
> It's different, but easy.
>
> You don't need buttons,
> It works like Access.
> To edit an editable windows datagrid row you just click on the row.
> You get nice edited icons to the left of each row and all sorts of stuff
> dead easy.
> Insertion?
> Click on that star row at the end of your grid.
>
> Tou can specify what's going on with the columns in code or there's a
> columns collection
> if you look under properties.
>
> I'm guessing you're binding to something that's read only.
> If you bind to a dataset/table you just get a load of functionality that
> you have to work for on an asp.net datagrid.
> This is VB but will give you the idea.
> http://www.code-magazine.com/Article.aspx?quickid=0301071
> Google "Introduction windows datagrid" for a shed load more topics.
> ( Do that first next time mate ).
>
> All that retaining state and reducing load on the web server stuff largely
> goes out the window.
> So (small) datasets are a much more acceptable solution in windows than
> web IMO.
> Editing a dataset in a web app would be crazy because each time you submit
> there's like 3 versions of all your data flying down the wire.
> Then it's held on the web server.
> So you use datareaders or whatever.
> With windows the client reads data into a dataset, the 3 copies are held
> on the client and only the changed data needs to be written back.
> So strongly datasets are a dead easy solution and also a pretty good one.
>
> The odd thing to throw to the back of your mind is that there's implicitly
> a dataview which dotnet creates and sits between your table and the grid.
> For some manipulation you need to work with that.
> Or I did back in 2004 anyhow.
>
> Hope this helps.

From: Andy O'Neill on

"Kali" <noemail(a)address.com> wrote in message
news:Ok9NOvkrKHA.5896(a)TK2MSFTNGP04.phx.gbl...
> Thank you, Andy. Will take a look at the article. Some of the issues I'm
> having are the event handlers compared to asp.net... onitemdatabound, etc.
> I am able to add a combobox to a datagridview and populate its listitems,
> however, I'm not able to keep that combobox when binding the data from an
> existing set of data.

The way binding works in windows forms is different now, I think.
What I used to do is apply a style to the table.
I can't recall why, but there was an advantage to doing this in code rather
than the column collection.

Here's some old code which has a combo box in it.
=======
Dim ts1 As New DataGridTableStyle()

ts1.MappingName = "Salesmen"

ts1.AlternatingBackColor = Color.LightBlue

Dim TextCol_0 As New DataGridTextBoxColumn()

TextCol_0.MappingName = "Salesman_Id"

TextCol_0.HeaderText = "Salesman Id"

TextCol_0.Width = 70

TextCol_0.TextBox.MaxLength = 8

ts1.GridColumnStyles.Add(TextCol_0)

'=========

Dim cboReg As New DataGridBoundComboColumn

cboReg.MappingName = "Region_Id"

cboReg.HeaderText = "Region"

cboReg.Width = 120

Try

Dim sconn As String = Conn_String()

Dim conn As SqlConnection = New SqlConnection(sconn)

Dim SqlString As String = "select null as region_id, '(Null)' as Region " &
_

"Union " & _

"Select Region_id, Region from Regions " & _

"order by Region"

Dim daRg = New SqlDataAdapter(SqlString, conn)

Dim dsRg = New DataSet

daRg.Fill(dsRg, "Regions")

With cboReg.ColumnBoundComboBox

..DataSource = dsRg.Tables("Regions").DefaultView

..DisplayMember = "Region"

..ValueMember = "Region_id"

End With

Catch ex As SqlException

MsgBox("Error reading Regions " & ex.Number & " " & ex.Message())

End Try

ts1.PreferredRowHeight = cboReg.ColumnBoundComboBox.Height + 3

ts1.GridColumnStyles.Add(cboReg)

'=====

Dim TextCol_3 As New DataGridTextBoxColumn

TextCol_3.MappingName = "Salesman_Name"

TextCol_3.HeaderText = "Salesman Name"

TextCol_3.Width = 110

TextCol_3.TextBox.MaxLength = 30

ts1.GridColumnStyles.Add(TextCol_3)

Dim TextCol_4 As New DataGridBoolColumn

TextCol_4.MappingName = "Manager"

TextCol_4.HeaderText = "Manager"

TextCol_4.Width = 50

TextCol_4.AllowNull = False

TextCol_4.FalseValue = "N"

TextCol_4.TrueValue = "Y"

ts1.GridColumnStyles.Add(TextCol_4)

Me.grdSalesmen.TableStyles.Add(ts1)

From: Andy O'Neill on

"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