473,669 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Primary Key in DataGrid

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:BoundColum n DataField="Prod uctId" Visible="false" ></asp:BoundColumn >
<asp:BoundColum n DataField="Cust omerId" 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.Ce lls(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
Nov 18 '05 #1
3 2375
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=grd List.DataKeys[x];
dsListe.ElisteU runRow
ElisteUrunRow=( dsListe.ElisteU runRow)TempElis te.Rows.Find(Da tabaseKey);

"Ryan Riddell" <Ry*********@di scussions.micro soft.com> wrote in message
news:20******** *************** ***********@mic rosoft.com...
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:BoundColum n DataField="Prod uctId" Visible="false" ></asp:BoundColumn >
<asp:BoundColum n DataField="Cust omerId" 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.Ce lls(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

Nov 18 '05 #2
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" <Ry*********@di scussions.micro soft.com> wrote in message
news:7F******** *************** ***********@mic rosoft.com...
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:EditComman dColumn ButtonType="Lin kButton" UpdateText="Upd ate" CancelText="Can cel" EditText="Edit" /> <asp:BoundColum n DataField="Prod uct" HeaderText="Pro duct" ReadOnly="true" >
<ItemStyle Font-Bold="True" />
</asp:BoundColumn >
<asp:BoundColum n DataField="Rang e" HeaderText="Ran ge" />
<asp:BoundColum n DataField="Prod uctId" Visible="true" ReadOnly="true" >
<HeaderStyle CssClass="Hidde n" />
<ItemStyle CssClass="hidde n" />
</asp:BoundColumn >
<asp:BoundColum n DataField="Cust omerId" Visible="true" ReadOnly="true" >
<HeaderStyle CssClass="Hidde n" />
<ItemStyle CssClass="hidde n" />
</asp:BoundColumn >
</Columns>
"Ryan Riddell" wrote:
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:BoundColum n DataField="Prod uctId" Visible="false" ></asp:BoundColumn > <asp:BoundColum n DataField="Cust omerId" 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.Ce lls(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

Nov 18 '05 #3
this worked for me:

<datagrid>
<asp:BoundColum n Visible="False" DataField="Empl oyeeID"
HeaderText="Emp loyeeID"></asp:BoundColumn >
.....

Sub dg_UpdateRow(By Val sender As Object,
ByVal e As DataGridCommand EventArgs)
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
<Ry*********@di scussions.micro soft.com> wrote:
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:BoundColu mn DataField="Prod uctId" Visible="false" ></asp:BoundColumn >
<asp:BoundColu mn DataField="Cust omerId" 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.Ce lls(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


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1479
by: Alpha | last post by:
Hi, I've worked with C# for 5 months fixing bugs for appl. done by other programmers. I'm just writing a small C# application by myself for the first time. The database is Access 2000. I've set up the OleConnection and selected the table that I want as a data source for the grid. My quesiton is, how can I display all columns except the primary key column whis is just an ID auto-generated by Access and has no meaning to the user, (I know...
7
2460
by: Dave | last post by:
Hi, Maybe I'm missing something with the DataKeyField attribute of a datagrid but it seems that it's somewhat limiting since this only allows you to specify one field as the key. I have a table that has two keys and it seems you need this value to find the corresponding record in the DataSet in order to update the data from the Datagrid.
1
1151
by: pmud | last post by:
I need an editable datagrid. But for editing a datagrid as shown in the walkthrough: Using a DataGrid Web Control to Read and Write Data, the table in the SQL database should have a primary key. But my table doesnt have one. So what is the best way I can create primary key in SQL database? Is UNIQUE IDENTIFIER a good option? Is it something like Auro Number of MS Access which is automatically filled by the database for each row? If not ,...
5
1864
by: Hennie | last post by:
When trying to update a record in an editable datagrid I ran into a few problems. My update procedure is just not working. Can someone please have a look at my code and see what am I doing wrong. I had to set the datagrid's DataKeyField to MailSubscriberID manually since vs does not do it automatically. Thanks
3
1724
by: Mark | last post by:
Hi everyone, could someone please recommend a way to do the following. I have a datagrid which contains a bound template column consisting of checkbox and a string pulled from a database table. I want to be able to associate the primary key of the database table with the checkbox so that when someone checks a box I can iterate through all the checkboxs and identify the associated value in the database. Thanks in advance Mark
3
2507
by: jaYPee | last post by:
i'm wondering how can i get the value of primary key in datagrid. thanks in advance
0
1066
by: Agnes | last post by:
For example, Invoice and InvoiceCharges Invoice No should be the primary key in Invoice Table, However, for the Invoice Charges, what primary key I should design ? I put 'recid' in the InvoiceCharges table, set it identity to Y and autoincrement in SQL Table Designer. In fact, I want to set recid as primary key in Charges Table. Too bad, I found that If I use data set and dataadapter to store the Invoice & charges information. If I...
3
2615
by: travlintom | last post by:
Hi all. I have a datagrid that has several columns including the Primary Key as the first column. When I add records to the grid the Primary Key column show "Null". This would not be a problem, but I want my users to be able to delete the column right away if they made a mistake with the entry. If the Primary key is null I can't access the dataset to get the ID number of the row to delete. Is there a way to refresh the datagrid...
3
1730
by: pragyasharma | last post by:
I am using datagrid . how can I identify the Item of the primary-key-field from database after clicking on a particular cell of datagrid ,which is not primary key field
0
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.