Mauricio Feijo's WebLog

Datagrid inserting rows question.

I am always looking for better solutions for a quick and clean code for table maintenance.

More for fun than anything I am writing a table maintenance page using datagrid. I am trying to keep it as abstract as possible, as I will later add other tables and reuse most code, or maybe wrap it in a custom control.

The datagrid is sorting asc and desc in all columns ( displaying an arrow showing the direction), allowing the user to pick how many rows to be displayed at a time, editing and deleting.

I want to make it insert too, and for that I have a button outside the datagrid, “Add Row“, that when clicked adds an empty row to the top of the dataset, saves the dataset, set currentpageindex and edititemindex and binds.

Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
SqlDataAdapter_store.Fill(DataSet_store)
Dim dr As DataRow = Me.DataSet_store.atr_store.NewRow
dr("store") = ""
dr("size") = ""
dr("active") =
True
Me.DataSet_store.atr_store.Rows.InsertAt(dr, 0)
Session("DataSet_store") = DataSet_store
dgStores.CurrentPageIndex = 0
dgStores.EditItemIndex = 0
viewstate("Operation") = "INSERT"
Binddata(
True)
End Sub

The problem I am finding is with the primary key column.
Before get to the insert part, I had it as a bound column, only showing the data (“store“). But using this approach to insert rows I need to have a textbox in the edititemteplate tag in that column, and the column needs to be a template column instead of databound column. All obvious, no problem.

But now when editing the “store“ textbox is available too.

I cannot make it readonly or disabled or invisible because either it hasn't been rendered yet and it doesn't exist, or it has been rendered and then it is too late to change it's attributes.

Also, I am trying to avoid adding client script to change the box attribute to read only after the page loads. I don't think it is elegant, and will probably byte me back..

I hate asking obvious questions about simple scenarios, but someone have a word on this?

Thanks

 

-Mauricio

Posted: Feb 04 2004, 05:21 PM by Mauricio Feijo | with 9 comment(s)
Filed under:

Comments

Johnny Hall said:

Why does this need to be a datagrid column at all? I can't really see any need for it to be displayed, and certainly not editable.

Use the DataKeyField property of the grid and (depending on the type of key you are using) set the primary key policy on the DataTable, ie. tell it that it is a autoincrementing field, or insert a uniqueidentifier yourself.
# February 4, 2004 5:57 PM

Mauricio Feijo said:

Thanks for your reply.

This datagrid lists a table with store ( store number), size ( store size) and an "Active" checkbox that defines if the row should be taken in consideration in a given process somewhere else in the system.

The store number needs to appear, needs to be seen for the table to make sense to the user, and it also needs to be entered when inserting rows. It must not be edited when editing a row. Just size and acgive can be edited.

Using the Datakey field property and setting the primary key on the datatable would be helpfull on that scenario?

THanks,

-Mauricio



# February 4, 2004 6:10 PM

Johnny Hall said:

I would suggest that "store" should not be your primary key. In *every* database that I design, and have seen designed, there is a non-meaningful pk, usually either a sequence field (oracle) or autoincrementing or guid-based (sql server). This is managed and handled by code only.

The "store" field is added by the user, but once added, it cannot be edited. Yes?

One way to do this would be to do this inside the TemplateColumn...

readonly='<%# System.Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "store")==String.Empty) %>'

Which would allow it to be edited when it is empty, but not when it is not. You should add a required field validator also.

I'm sure there are other ways to do this. But I'm sure this'll work.

(I've not coded this up to check the syntax - you may be better to wrap the code up into a function).
# February 4, 2004 6:34 PM

Johnny Hall said:

The readonly property would be on your TextBox.
# February 4, 2004 6:35 PM

Johnny Hall said:

And it would be !=String.Empty ! Sorry.
# February 4, 2004 6:56 PM

Mauricio Feijo said:

I like it a lot. I'll try it and post something back. Thakns!
# February 4, 2004 7:13 PM

Mauricio Feijo said:

Worked perfectly. Thanks!

About the Architecture, the reason why I cannot use Foreign Key constraints on this case is that the Main store table is merely a SQL copy, a SQL version that gets recreated from scratches every morning.
The master data, that is locked up somewhere, is in DBF format.

I am changing this structure to have a process synchronizing the tables every morning after the SQL copy gets created. That will do
# February 5, 2004 12:33 PM

Johnny Hall said:

No problem.
# February 5, 2004 2:22 PM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 5:38 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)