Connecting Tech Pros Worldwide Forums | Help | Site Map

Primary Key in DataGrid

Ryan Riddell
Guest
 
Posts: n/a
#1: Nov 18 '05
I am using the DataGrid control from 4guys (http://aspnet.4guysfromrolla.com/dem...hlighter.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

Ersin Gençtürk
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Primary Key in DataGrid


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.Ro ws.Find(DatabaseKey);



"Ryan Riddell" <RyanRiddell@discussions.microsoft.com> wrote in message
news:20E4D38D-851D-42A0-9811-EE7D86B5864E@microsoft.com...[color=blue]
> I am using the DataGrid control from 4guys[/color]
(http://aspnet.4guysfromrolla.com/dem...hlighter.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.[color=blue]
>
> The second requirement is that the grid stores 2 table primary key values[/color]
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:[color=blue]
>
> <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[/color]


Ersin Gençtürk
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Primary Key in DataGrid


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" <RyanRiddell@discussions.microsoft.com> wrote in message
news:7F1F30D9-8DFB-4C49-BEE9-87C41BEA6169@microsoft.com...[color=blue]
> 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[/color]
headerstyle CssClass attribute to "hidden".[color=blue]
>
> <Columns>
> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"[/color]
CancelText="Cancel" EditText="Edit" />[color=blue]
> <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>
>
>
> "Ryan Riddell" wrote:
>[color=green]
> > I am using the DataGrid control from 4guys[/color][/color]
(http://aspnet.4guysfromrolla.com/dem...hlighter.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.[color=blue][color=green]
> >
> > The second requirement is that the grid stores 2 table primary key[/color][/color]
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:[color=blue][color=green]
> >
> > <asp:BoundColumn DataField="ProductId"[/color][/color]
Visible="false"></asp:BoundColumn>[color=blue][color=green]
> > <asp:BoundColumn DataField="CustomerId"[/color][/color]
Visible="false"></asp:BoundColumn>[color=blue][color=green]
> >
> > Then in the update command I simply Response.Write out the values for[/color][/color]
now:[color=blue][color=green]
> > 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[/color][/color]


hansiman
Guest
 
Posts: n/a
#4: Nov 18 '05

re: Primary Key in DataGrid


this worked for me:

<datagrid>
<asp:BoundColumn Visible="False" DataField="EmployeeID"
HeaderText="EmployeeID"></asp:BoundColumn>
.....

Sub dg_UpdateRow(ByVal sender As Object,
ByVal e As DataGridCommandEventArgs)
Dim tb As TextBox = e.Item.Cells(0).Controls(0)
Dim iEmployeeID As String = tb.Text
.....

/Morten




On Wed, 28 Jul 2004 06:45:03 -0700, Ryan Riddell
<RyanRiddell@discussions.microsoft.com> wrote:
[color=blue]
>I am using the DataGrid control from 4guys (http://aspnet.4guysfromrolla.com/dem...hlighter.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[/color]

Closed Thread