AndroidError
AndroidError

Reputation: 21

c# (vbs-15) how to fill datagrid and provide links

Sorry guys, but am new to c# and vbs. what am trying to do is to fill datagrid or any equivalent then i need to provide click-able buttons/links to each row.

When user click the button, the app should pass the row-id clicked to new (another) form.

...
            p.textquery = "SELECT * FROM members ORDER BY member_id DESC LIMIT 250";
            SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=" + p.dbname() + ";Version=3;");
            m_dbConnection.Open();
            try
            {
                SQLiteCommand command = new SQLiteCommand(p.textquery, m_dbConnection);
                SQLiteDataReader reader = command.ExecuteReader();
                bool rows = reader.HasRows;
                if (rows)
                {

                    while (reader.Read())
                    {
                        patient_id = reader["patient_id"].ToString();
// Here i need to show results to user but the user should have a button to select the row
                    }
                    return;
                }
                else
                {
                    p.alert("You do not have members in the system");
                    return;
                }
            }
            catch
            {
                p.alert("SQL has error - (database error)");
                return;
            }
...

The problem is how to provide datagrid or any equivalent which has button where user can click to view the record.

Your help is highly appreciated

EDIT

private void dataGridView1_CellContentClick_2(object sender, DataGridViewCellEventArgs e)
{
    plugin p = new plugin();
    try {
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewButtonCell)
        {
            //I need the value of the column here member_id is only showing the rowindex and columnindex now i need the value
            p.alert(dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].ToString());

        }
        else
        {
            p.alert(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToString() + " on else");
        }
    }
    catch (Exception v)
    {
        //p.alert("Header clicked ");
    }
}

EDIT - ANSWER

dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

where Cells[2] is the targeted cell

Upvotes: 0

Views: 156

Answers (1)

Krishna
Krishna

Reputation: 1985

Drag and drop a datagridview from the toolbox to the window, Add All the columns in your data table to it using properties--> columns in that add a button column or a link column. Bind data to grid as below

DataTable dtMembers = new DataTable();
p.textquery = "SELECT * FROM members ORDER BY member_id DESC LIMIT 250";
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=" + p.dbname() + ";Version=3;");
try
{
    SQLiteDataAdapter da = new SQLiteDataAdapter(p.textquery,m_dbConnection);
    m_dbConnection.Open();
    da.Fill(dtMembers);
    m_dbConnection.Close();
    if(dtMembers.Rows.Count > 0)
        dgvMembers.DataSource = dtMembers;
    else
        p.alert("You do not have members in the system");
}
catch
{
    p.alert("SQL has error - (database error)");
    return;
}

Implement DataGridView cell content click

private void dgvMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if(dgvMembers.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewButtonCell)
    {
        //Write your code here
    }
}

See below I have text property where I wrote click here, that's where you need to put button name enter image description here

Upvotes: 0

Related Questions