String DataKeyNames for string[] datasource

 Hi,

I am having a problem with a gridview.  I am binding an array of strings to the gridview and then having a problem setting the DataKeyNames.  Because the dataItem is of type string I cant place the name of any property in DataKeyNames property.

gridView.DataSource = new string[] { "one", "two" };
gridView.DataBind();

<asp:GridView runat="server" ID="gridView" DataKeyNames="???">

<Columns>

...

</Columns>

</asp:GridView>

The only solution I can thing of is to convert the string array into an array of Postcode entities that has a Value property, however I dont really want to do that if there is a better way!?  Perhaps we can set the DataKey on databound some how?

Thanks in advance!


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
6/18/2008 10:51:43 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

3 Replies
735 Views

Similar Articles

[PageSpeed] 41

You can place any property of the element in the array (string) in the DataKeyNames. The only applicable property here is Length.

Mark replies as answers if they helped you solve the problem.
0
siva_sm
6/18/2008 11:55:13 AM

siva_sm:
You can place any property of the element in the array (string) in the DataKeyNames. The only applicable property here is Length

Yes I could but i want the value not the length!


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
6/18/2008 12:38:32 PM

 You can do it by using an anynymous type & linq.  kinda dumb, but it works:

 EG

string [] files = ...;

var list = from f in files
   
select new { Letter = f };

// anonymous type created with member called Letter

lv
.DataKeyNames = "Letter";
lv
.DataSource = list;
lv
.DataBind();
 

See: http://stackoverflow.com/questions/253911/listview-can-you-set-datakeynames-when-binding-to-a-simple-scalar-array

0
jshallard
2/5/2009 9:27:51 PM
Reply:

Similar Artilces:

string to string[]
Hi, I've got this code : string[] Params; string SQL = "SELECT * FROM T_MANAGEMENT_PAGES"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Params_Type = myReader.GetValue(0).ToString(); } } catch { } finally { } myReader.Close(); My problem is to obtain Params_Type. But each time, it says : "impossible to convert '[object]' in 'string[]' &quo...

string() = string()
this code does not work (not instance of object) Dim Items(), Other() As String Items = New String() {"A","B","C"} Other = New String() {} Other = Items how can I get all the values ? angiras Hi, If I understand your requirement correctly, this should do it Dim Items() As String = {"A", "B", "C"} Dim Other(Items.Length - 1) As String Items.CopyTo(Other, 0) To more closely match you code above, but this will be less efficient Dim Items(), Other() As String ...

string and string[]
hello, whats the difference here between string and string[] thanks "string" denotes an item of type String "string[]" denotes an array of type String itemsThanks, EdMicrosoft MVP - ASP/ASP.NET Gracia mon ami...

String and String[]
What will be the advantage of using myString: String[10] over myString: String Man wrote: > What will be the advantage of using > myString: String[10] > over > myString: String "String[10]" is a fixed-length, statically allocated, Ansi-based ShortString. "String" is a dynamic-length, dynamically allocated, reference counted, Ansi-based or Unicode-based (depening on Delphi version) Long string. Unless you absolutely need the static, fixed length nature of ShortString, such as in records that interact with external systems, you should stay ...

String and string?
Hi guys, I was wondering the difference between small letter string type and a capital letter String type. Does anyone know about this? Highly appreciate for your answers! Thankz! The "proper" version is String, as it references the System.String class of the .NET Framework Class Library. The ability to use string is provided by C#, where it is an alias to String. Similarly, the "proper" version of an integer is Int32, as it references System.Int32.  The ability to use int is provided by C#, and the ability to use Integer is provided by VB.NET, and both int and Integer are aliases...

a string in a string in a string in my code behind....
I know my title talk about code-behind, but this is really a client-side problem (and surely a newby one too, sorry about that).I have a javascript function that my make a little text appear when the onmouseover event of a link is activated.So, normally, if my link was inside my html code, it would be something like this:<a href = "whocare.com"  onmouseover="poplink('I want to test something');">test</a>BUT, for some reason, I have to write this part in code behind with the LiteralControl function....and that will give something like that:this.Controls.Add(new LiteralCo...

m_AVP As IDictionary(Of String, String) = New Dictionary(Of String, String) ,not defined?
   I am creating a usercontrol (ascx,ascx.vb)   I have used Imports system.Collections Imports system.Collections.Generic     Private m_AttributeValuePair As IDictionary(Of String, String) = New Dictionary(Of String, String)     But I am gettting an error “Type Dictionary is not defined”   And also I am getting intelisense to type the line But what is the reason for my error ? How can I get escape from the error ?         this error is not showing today  ...

How to copy strings in array of strings....I am getting some errors....
Hi, I want to display all the file names that are present in a specified directory....say "C:\data" in a GridView for that I am using the following: Imports System.IOPartial Class _Default Inherits System.Web.UI.PageProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strFileSize As String = ""Dim di As New IO.DirectoryInfo("C:\data") Dim aryFi As IO.FileInfo() = di.GetFiles()Dim fi As IO.FileInfo For Each fi In aryFi strFileSize = (Math.Round(fi.Length / 1024)).ToString() Response.Write(fi.Name)...

String Manipulation
 Hello all, Using VB I'm trying to have a multiline textbox that a user will put in a quantity and a name as follows: 3xApple2xPear4xOrangeAnd when they click a button I want each part of this as a separate value, so far I have the following: Function GetSingleCards(ByVal AllFruits As String) As String Dim OurFruits As String() = Nothing Dim SingleFruit As String Dim ReturnThis As String OurFruit = AllFruits.Split(vbCrLf) For Each SingleFruit In OurFruits ReturnThis &= SingleFruit & ", " '...

How to replace string starting with "<!-- Start Here" and ending with "End Here-->" with another string?
I found a site yesterday that showed how to do this, but never saved it. For the life in me I can't seem to find it again. I'm sure its a function of some string class but I can't find it. In a much larger string I need to replace a string starting with "<!-- Start Here" and ending with "End Here-->" with another string? ThanksUsing Visual Studio 2005. Language: C# string s = "<!-- Start Here This is a test End Here-->"; s = s.Replace("<!-- Start Here ", ""); s = s.Replace(" End Here-->", "&qu...

How to Encrypt a String to a String?
Hi Folks,I’m using Triple DES to encrypt a string… the output of the encryption is a byte array.However, I want to store the encrypted result as a string in a SQL Database and not as a byte array.(The reason I want to store it as a string, is to be able to copy & paste the result to a standalone windows form application  to decrypt it...) I’ve tried taking the byte array, converting it into a string…And then reconverting it into a byte array for the decryption. This causes an error upon decryption…How can I convert the encrypted byte array into a string and back into a byt...

RSA Encryption
Here is my goal:1. Take a string2. Encrypt it 3. Pass it as a parameter in the QueryString4. Decrypt it The value starts as a string, then is converted to a byte[] and then encrypted.  The resulting byte[] is converted to a string and send as a parameter.  The recieving page decrypts the string (creates a byte[], decrypts to a new byte[], and the value is finally parsed for its values) I am using http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrsacryptoserviceproviderclasstopic.asp with a small modification t...

Difference between String and string
When would you use String as apposed string, when writing functions certain functions work when defined as private string help ( string param){ } Others require the following private string help ( String param){ } Does anybody know why? In C#, string is an alias for the .NET Framework provided type String (and recall C# is case sensitive). I am not aware of any compelling reason to use one vs. the other.Starting with ASP.NET 2.0? Look at:Programming Microsoft Web Forms My Blog...

String and String[30]
Any advantage in using this style in defining a string type variable? myName: String; hisName: String[30]; "Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message news:177975@forums.codegear.com... > Any advantage in using this style in defining a string type variable? > > myName: String; > hisName: String[30]; String[30] is a short string type and is ANSI only. You should use simple String type (Unicode in D2009+). -- Wayne Niddery (TeamB) Alan Tse wrote: > Any advantage in using this style in defining a string type variable? ...

Web resources about - String DataKeyNames for string[] datasource - asp.net.getting-started

moredotnet
M ore d otnet .NET Interview Questions, ASP.NET, C#, VB.NET, XML, SQL Check Out http://www.dotnetUncle.com √ .NET NOTES OOPS in .NET .NET ...

Resources last updated: 12/25/2015 9:32:14 PM