0

I have two textbox where user can enter No of Rows and No of Columns. and one dropdown for Abbrevation as "A-Z"

So if user enters

1st textbox --- 5

2nd textbox --- 5

and selects abbr as "A-Z"

So in gridview it should display as

Gridview display

any idea pls suggest how to start.

Nad
  • 4,605
  • 11
  • 71
  • 160
  • You also want the curves? Sorry, i could not resist :) – Tim Schmelter Nov 20 '15 at 12:07
  • @TimSchmelter: its just a diagrammatic representation. just need the matrix – Nad Nov 20 '15 at 12:08
  • This is too broad. You haven't shown what you've tried so far. So there's currently no issue to fix but just a requirement for us. – Tim Schmelter Nov 20 '15 at 12:10
  • @TimSchmelter: I only want ideas, that how to combine the abbrevation and display. If any please suggest. I m still not getting any errors – Nad Nov 20 '15 at 12:11
  • You could fill a `DataTable` with rows and columns according to the user input. `int cols=int.Parse(txtCol.Text);for(int i=0; i – Tim Schmelter Nov 20 '15 at 12:13
  • @TimSchmelter: `A1` will contain only rows or columns, I want both Rows and columns, so that's y `A11`. Right now we are not concerned about when it exceds 9. – Nad Nov 20 '15 at 12:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95697/discussion-between-coder-and-tim-schmelter). – Nad Nov 20 '15 at 12:25

1 Answers1

1

You can use a DataTable as DataSource and fill it in this way:

protected void btnDisplay_Click(object sender, EventArgs e)
{
    // use CompareValidators for the two TextBoxes for rows and columns
    // with it's Operator property set to DataTypeCheck and Type="Integer"
    int rows = int.Parse(txtNoOfRowsRC.Text);
    int columns = int.Parse(txtNoOfColRC.Text);

    grdBinDefinitionDisplay.DataSource = GetDataSource(rows, columns);
    grdBinDefinitionDisplay.DataBind();
}

protected DataTable GetDataSource(int rows, int columns)
{
    DataTable table = new DataTable();
    for (int c = 1; c <= columns; c++)
        table.Columns.Add("Column " + c.ToString());

    for (int r = 1; r <= rows; r++)
    {
        DataRow row = table.Rows.Add();
        foreach (DataColumn col in table.Columns)
        {
            string value = string.Format("{0}{1}{2}", IntToLetters(r), r, col.Ordinal + 1);
            row.SetField(col, value);
        }
    }
    return table;
}

I have used this method to generate the letter for the row-number(rewards here):

public static string IntToLetters(int value)
{
    string result = string.Empty;
    while (--value >= 0)
    {
        result = (char)('A' + value % 26) + result;
        value /= 26;
    }
    return result;
}

This handles the case with abbrevation as "A-Z", you haven't mentioned any other.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • is that to be added under button click ?? – Nad Nov 20 '15 at 12:38
  • @coder: that has to be added to a method that will be called from the button-click event handler. And of course you need to add `myGridView.DataSource=GetDataTable();myGridView.DataBind();`. Btw, rewards for the `IntToLetter` goes to: http://codereview.stackexchange.com/questions/13105/integer-to-alphabet-string-a-b-z-aa-ab – Tim Schmelter Nov 20 '15 at 12:40
  • what is `GetDataTable` here. Here is what I tried https://dotnetfiddle.net/Pz7Uy8 – Nad Nov 20 '15 at 12:47
  • @code: it was just the method that i have mentioned. In your code it is `DisplayGrid`. But i would give it a different name since it generates and returns the DataSource as `DataTable`. It doesn't show it. – Tim Schmelter Nov 20 '15 at 12:50
  • i have written the method, just i m stuck how to bind the gridview. I m confused with `GetDataTable` :(, Also see my aspx https://dotnetfiddle.net/UcNDuT – Nad Nov 20 '15 at 12:53
  • ok. is my aspx fine for the gridview to display.. ` ` – Nad Nov 20 '15 at 12:56
  • @coder: you also have to set `AutoGenerateColumns="true"` – Tim Schmelter Nov 20 '15 at 12:57
  • other than that do I need to **add or remove** something from the grid ?? – Nad Nov 20 '15 at 13:04
  • actually I debug the code, and i got error as `cannot read property firstChild of null` – Nad Nov 20 '15 at 13:12
  • @coder: it seems that this error is not related to my code because it's a javascript error. http://stackoverflow.com/questions/22650699/javascript-uncaught-typeerror-cannot-read-property-firstchild-of-null – Tim Schmelter Nov 20 '15 at 13:13
  • Its not related to your code but it says that `id` is null or something like that. but i havent used any javascript on the page..isn't it strange ? – Nad Nov 20 '15 at 13:15
  • I resolved that error, actually when I debug the code, a blank area was coming on pageload may be that was due to grid. I made `visible= false` and now that error is gone. Now how to make it visible. After `grdBinDefinitionDisplay.DataBind();` right ?? – Nad Nov 20 '15 at 13:22
  • @coder: i don't know what code you use. That's clearly a javascript issue which isn't related to this question at all. Maybe you should ask a different question according to that new issue. If i use my code on a blank ASP.NET page it works without a problem. – Tim Schmelter Nov 20 '15 at 13:26
  • absolutely it works perfectly. now my gridview is also been displayed. its just the height and width matters. I will look at it by my own. Thanks a lot Tim. the answer is very much perfect. +1 for handling `A-Z` part. – Nad Nov 20 '15 at 13:28
  • just one thing, how can we set column `width` from the code behind **here** – Nad Nov 20 '15 at 13:35
  • 1
    @coder: you can set it in `RowCreated` or `RowDataBound` event. `e.Row` is the `GridViewRow` and `e.Row.Cells` contains all columns. If you want to set the first cell's width: `e.Row.Cells[0].Width = New Unit(120);` (f.e. if you want it to have 120 pixels) – Tim Schmelter Nov 20 '15 at 13:47
  • :Ok got that. One more thing,there is a slight change in the result. Earlier I wanted to look it as `A11` now I want to look it as `A-1-1` how to insert that `-` in between them ? – Nad Nov 21 '15 at 05:00