MoralesJosue
MoralesJosue

Reputation: 199

Create a tab for each row found in db

I have a query that selects a table form my db and displays it on some labels, but if the query finds more the one data row it has to display each one of them in a different tab of the form, how can i archive this.

Code:

try
{
    mycon.Open();

    string querieslabels = "Select *From Alerts where State = @state && Lv =@lv";

    oleDbCmd = new OleDbCommand(querieslabels, mycon);

    oleDbCmd.Parameters.Add("@state", OleDbType.VarChar, 10).Value = "Open";
    oleDbCmd.Parameters.Add("@lv", OleDbType.VarChar, 2).Value = "1";

    OleDbDataReader read1;

    read1 = oleDbCmd.ExecuteReader();

    read1.Read();

    state = read1[4].ToString();

    if (state != "Closed")
    {
        lblNum.Text = read1[0].ToString();
        lblpress.Text = read1[1].ToString();
        txtDescription.Text = read1[2].ToString();
        lblDate.Text = read1[3].ToString();

        lblOk.Visible = false;
        pictureBox2.Visible = false;
        mycon.Close();
    }
    else
    {
        //Hides labels and other control's
        MakeNotVisible();

        MessageBox.Show("No alert found");
    }
    catch (Exception error)
    {
        MessageBox.Show(error.ToString());
    }
    finally 
    { 
        mycon.Close(); 
    }
}

What I'm trying to create is some alert system, if there is more than 1 alert it should display in a different tab. Can some one guide me to the solution for this, any help or suggestion is appreciated.

NOTE: I'm using Win Form.

Upvotes: 0

Views: 828

Answers (2)

TaW
TaW

Reputation: 54433

You could create a UserControl, with code that can display one record from a DataTable. Then you can create TabPages and add one new UC to it for each of your rows. Take care not to add too many pages, say no more than 10-15!

Update I just noticed that you are using a DataReader, not a DataTable. No problem, the same solution will worke here, too: Use the added ShowReader method and call it like below..!

Here is an example:

public partial class DataForm : UserControl
{
    public DataTable theTable { get; set; }

    public DataForm()
    {
        InitializeComponent();
    }

     public int  showTableRow(int rowIndex)
     {
         if (theTable == null) return -1;
         if (theTable.Rows.Count < rowIndex) return -2;

         DataRow row = theTable.Rows[rowIndex];
         label1.Text = row[0].ToString();
         label2.Text = row[1].ToString();
         label3.Text = row[2].ToString();

         return 0;
     }

    public void showReaderRow(OleDbDataReader DR)
    {
        label1.Text = DR[0].ToString();
        label2.Text = DR[1].ToString();
        label3.Text = DR[2].ToString();
    }
}

For easier access we'll keep a List:

List<DataForm> theDataPages = new List<DataForm>();

Now we can add DataForm pages to the Tab control tabFormPages from the records in a DataTable; here I limit the number to 5:

for (int r = 0; r < DTL.Rows.Count && r < 5; r++)
{
    TabPage page = new TabPage( (r+1).ToString("Record #0" ));
    DataForm aForm = new DataForm();
    aForm.theTable = DTL;
    aForm.Parent = page;
    aForm.showTableRow(r);
    tabFormPages.TabPages.Add(page);
    theDataPages.Add(aForm);
}

For use with a DataReader you could write a read loop like this:

while (DR.Read()) 
{
    TabPage page = new TabPage("Alert #" 
                 + (tabFormPages.TabPages.Count+1).ToString());
    DataForm aForm = new DataForm();
    aForm.Parent = page;
    aForm.showReaderRow(DR);
    tabFormPages.TabPages.Add(page);
    theDataPages.Add(aForm);
} 

First you add the class to your project: Add - Add Usercontrol.

Now you can open it in the Designer and put in all Labels you need as well as the and put a the code. Also flesh out the showRow method to properly display your data in those Labels..

Now you can add such UCs to TabPages in your Tab like I did above.

If you are using a DataTable you can also make one page display a certain record by calling the showRow again:

theDataPages[0].showRow(DTL.Rows.Count - 1);

This will show the last record in the first TabPage.

If you really need to access field on that UC you can create public properties for those fields and access them like so:

    public DataTable theTable { get; set; }
    public Label Label1 { get; set; }
    //..

    public DataForm()
    {
        InitializeComponent();
        Label1 = label1;
        //..
    }

Now you can access the field(s):

    if (theDataPages[0].Label1.Text == "Hiho" 
        theDataPages[0].Label1.BackColor = Color.Green;

Upvotes: 2

ArunGeorge
ArunGeorge

Reputation: 485

Are you using a WinForms application or a WebForms application? You might need an Accordion control (http://www.codeproject.com/Articles/416521/Easy-WinForms-Accordion-Control) or a Tab Control (http://www.c-sharpcorner.com/UploadFile/mahesh/C-Sharp-tabcontrol/) or something similar. You can dynamically create the tabs in them based on the number of rows in your result.

Upvotes: 1

Related Questions