Primary Key in DataGrid

G

Guest

I am using the DataGrid control from 4guys (http://aspnet.4guysfromrolla.com/demos/RowHighlighter.aspx). I want to display 2 columns and allow editing. The only column I want the user to be able to change is the second column. I made this work via ReadOnly="true" for the first column.

The second requirement is that the grid stores 2 table primary key values in columns 3 and 4. These columns will mean nothing to the user and could potentially confuse them. For this reason I need to hide the columns. So in the DataGrid definition on the .aspx page I added:

<asp:BoundColumn DataField="ProductId" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerId" Visible="false"></asp:BoundColumn>

Then in the update command I simply Response.Write out the values for now:
Dim s1 As String = e.Item.Cells(1).Text
Dim s2 As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim s3 As String = e.Item.Cells(3).Text
Response.Write(s1 + "__" + s2 + "::" + s3)

Doing this yields:
[correctColumn1]__[correctColumn2]:: <-s3 is blank

How do I get the values in the hidden third and fourth columns?

Thanks,
Ryan
 
E

Ersin Gençtürk

Hi ryan,

I would suggest you to use DataKeyField property of the datagrid column.So
you won't need to hide columns etc.

You can use it like this to get key field :


//int x = row number;

object DatabaseKey=grdList.DataKeys[x];
dsListe.ElisteUrunRow
ElisteUrunRow=(dsListe.ElisteUrunRow)TempEliste.Rows.Find(DatabaseKey);



Ryan Riddell said:
I am using the DataGrid control from 4guys
(http://aspnet.4guysfromrolla.com/demos/RowHighlighter.aspx). I want to
display 2 columns and allow editing. The only column I want the user to be
able to change is the second column. I made this work via ReadOnly="true"
for the first column.
The second requirement is that the grid stores 2 table primary key values
in columns 3 and 4. These columns will mean nothing to the user and could
potentially confuse them. For this reason I need to hide the columns. So
in the DataGrid definition on the .aspx page I added:
<asp:BoundColumn DataField="ProductId" Visible="false"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerId" Visible="false"></asp:BoundColumn>

Then in the update command I simply Response.Write out the values for now:
Dim s1 As String = e.Item.Cells(1).Text
Dim s2 As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim s3 As String = e.Item.Cells(3).Text
Response.Write(s1 + "__" + s2 + "::" + s3)

Doing this yields:
[correctColumn1]__[correctColumn2]:: <-s3 is blank

How do I get the values in the hidden third and fourth columns?

Thanks,
Ryan
 
E

Ersin Gençtürk

why my method didn't work ? did you tried it well ?
anyway , it is the suggested way to get primary keys from the datagrid.I
think you should try it again.

regards

Ryan Riddell said:
Neither method worked for me.

Instead I defined a style in the sheet.
<head>
<style type="text/css">
.hidden
{
display: none;
}
</style>
</head>

Then I statically defined the columns and set the itemstyle and
headerstyle CssClass attribute to "hidden".
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit" />
<asp:BoundColumn DataField="Product" HeaderText="Product" ReadOnly="true">
<ItemStyle Font-Bold="True" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Range" HeaderText="Range" />
<asp:BoundColumn DataField="ProductId" Visible="true" ReadOnly="true">
<HeaderStyle CssClass="Hidden" />
<ItemStyle CssClass="hidden" />
</asp:BoundColumn>
<asp:BoundColumn DataField="CustomerId" Visible="true" ReadOnly="true">
<HeaderStyle CssClass="Hidden" />
<ItemStyle CssClass="hidden" />
</asp:BoundColumn>
</Columns>
(http://aspnet.4guysfromrolla.com/demos/RowHighlighter.aspx). I want to
display 2 columns and allow editing. The only column I want the user to be
able to change is the second column. I made this work via ReadOnly="true"
for the first column.values in columns 3 and 4. These columns will mean nothing to the user and
could potentially confuse them. For this reason I need to hide the columns.
So in the DataGrid definition on the .aspx page I added:
<asp:BoundColumn DataField="ProductId"
Visible="false"> said:
<asp:BoundColumn DataField="CustomerId"
Visible="false"> said:
Then in the update command I simply Response.Write out the values for now:
Dim s1 As String = e.Item.Cells(1).Text
Dim s2 As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim s3 As String = e.Item.Cells(3).Text
Response.Write(s1 + "__" + s2 + "::" + s3)

Doing this yields:
[correctColumn1]__[correctColumn2]:: <-s3 is blank

How do I get the values in the hidden third and fourth columns?

Thanks,
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top