HEEN
HEEN

Reputation: 4735

Display gridview on the basis of columns and rows entered

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.

Upvotes: 0

Views: 769

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460380

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.

Upvotes: 1

Related Questions