0

For example, I have a record of quizzes. I want to display all of the quizzes along with the student who took it. If the total number of quizzes is 20, then the list-view would dynamically add another column to the list-view. How can I do that? Also, I want the 1st column of the list-view to display the name of the students.

This is how I manually add the items to the column.

listView1.Items.Clear();
conek.OPEN("Select * from thesisdb.teachers where username='"+txts+"'");
while(conek.reader.Read()){
        ListViewItem ni = new ListViewItem(conek.reader[0].ToString());
        ni.SubItems.Add(conek.reader[1].ToString());
        ni.SubItems.Add(conek.reader[2].ToString());
        ni.SubItems.Add(conek.reader[3].ToString());
        ni.SubItems.Add(conek.reader[4].ToString());
        ni.SubItems.Add(conek.reader[5].ToString());
        ni.SubItems.Add(conek.reader[6].ToString());
        ni.SubItems.Add(conek.reader[7].ToString());
        ni.SubItems.Add(conek.reader[8].ToString());
        ni.SubItems.Add(conek.reader[9].ToString());
        listView1.Items.AddRange(new ListViewItem[] { ni }); 
}
conek.CLOSE();

I tried using a for loop for the number of items for the reader but it doesn't seem to be working.

this class was given by our instructor and this is what we have been using ever since we started in c#.

namespace spms.classes
{
public class clsOpenCon
{
    public static string connectionAddress = "uid=root; database=thesisdb;";
    public static MySqlConnection CN = new MySqlConnection(classes.clsOpenCon.connectionAddress);

    public MySqlCommand Com = new MySqlCommand();
    public MySqlDataReader reader;

    public void nonQuery(string cmdText)
    {
    Com.Connection = CN;
    CN.Open();
    Com.CommandText = cmdText;
    Com.ExecuteNonQuery();
    Com.Dispose();
    CN.Close();
    }
    public void OPEN(string cmdtext)
    {
    Com.Connection = classes.clsOpenCon.CN;
    classes.clsOpenCon.CN.Open();
    Com.CommandText = cmdtext;
    reader = Com.ExecuteReader();
    }
    public void CLOSE()
    {
    reader.Close();
    Com.Dispose();
    classes.clsOpenCon.CN.Close();
    }
    }
    }

1 Answer 1

2

You might find it easier to use a DataGridView and use a DataTable to populate it automatically, like this:

dataGridView.DataSource = conek.ExecuteQuery("Select * from thesisdb.teachers where username='"+txts+"'");

Where ExecuteQuery looks like this:

internal DataTable ExecuteQuery(string query)
{
    DataTable table = new DataTable();

    var refDataAdapter = new MySqlDataAdapter(new MySqlCommand(query, SqlConn));
    refDataAdapter.Fill(table);

    return table;
}

But to answer your question you need to add the columns yourself by calling listview1.Columns.Add before you add the item.

In addition to your question you shouldn't be pasting text into your query like txts but instead using parameters to avoid an SQL injection attack.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm still quite new with c# and i haven't used datagridview before. does the logic on adding sub-items and item range the same with the listview?
A datagridview will do it all for you - all you do is pass it the DataTable - that line I've put in my answer is really all you'll need! It'll get the column names and add them for you. You might want to set a bit of formatting in the GUI designer to make it look like you want - you can make them look a lot like a listview. If you want to do it manually best to stick with the listview probably - datagridview's are best for the automatic way!
so i just have to create a data table in which i should write down my sql statements then use the data table for the datagridview to display the data?
my conek is actually an instance to a class for the database connection. hang on let me show it to you
OK - added a function that will return a DataTable - a good function to add to your database class.
|

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.