1

I want to bind 2d array to gridview. How can do so. I am attaching my code also. If there is any other way then also suggest me.

        string sQS;
        string[] aQS;
        string pwd = "ebskey";
        string DR = Request.QueryString["DR"].ToString();
        DR = DR.Replace(' ', '+');
        sQS = Base64Decode(DR);
        DR = EBSHelper.Decrypt(pwd, sQS, false);
        aQS = DR.Split('&');
        int p1 = aQS.Length;
        string[,] pp = new string[p1,2];
        for (int i = 0; i < aQS.Length; i++)
        {
            string[] aParam = aQS[i].Split('=');
            pp[i,0] = aParam[0].ToString();
            pp[i,1] = aParam[1].ToString();

        }
        GridView1.DataSource = pp;
        GridView1.DataBind();

2 Answers 2

2

As I understand your input string is like this:

var inputString = "key1=value1&key2=value2&key3=value3";

The code below will parse the string and make a list, and bind it to a grid.

var list = Regex.Matches(inputString, @"(?<key>\w+)=(?<value>\w+)(&|$)")
    .Cast<Match>()
    .Select(arg => new { Key = arg.Groups["key"].Value, Value = arg.Groups["value"].Value })
    .ToList();
GridView1.DataSource = list;
GridView1.DataBind();

So your final code would look like this:

string pwd = "ebskey";
string DR = Request.QueryString["DR"];
DR = DR.Replace(' ', '+');
string sQS = Base64Decode(DR);
DR = EBSHelper.Decrypt(pwd, sQS, false);

var list = Regex.Matches(DR, @"(?<key>\w+)=(?<value>\w+)(&|$)")
    .Cast<Match>()
    .Select(arg => new { Key = arg.Groups["key"].Value, Value = arg.Groups["value"].Value })
    .ToList();
GridView1.DataSource = list;
GridView1.DataBind();
Sign up to request clarification or add additional context in comments.

Comments

2

I would try something like this instead of a 2d array.

var data = aQS.Select(s => new {Col1 = s.Split('=')[0], Col2 = s.Split('=')[1]});
GridView1.DataSource = data;
GridView1.DataBind();

I don't like calling Split() twice but I'm not sure how to work around that.

or you can keep things really straighforward and create a simple class

class RowData{
    public string Col1Value {get; set;}
    public string Col2Value {get; set;}
}

and you can do

List<RowData> rows = new List<RowData>();
for (int i = 0; i < aQS.Length; i++)
 {
        string[] aParam = aQS[i].Split('=');
        RowData row = new RowData {Col1Data = aParam[0], Col2Data = aParam[1]};
        rows.Add(row);
 }

 GridView1.DataSource = rows;
 GridView1.DataBind();

4 Comments

The workaround to 'calling it twice' is 'calling it once' :).
@Alex Feel free to edit the answer if you can come up with a way to skip the second redundant call.
I think it is better not to make aQS array in the first place.
In your first version, you could do this to avoid double splits: var data = from s in aQS let split = s.Split('=') select new { Col1 = split[0], Col2 = split[1] };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.