none
Gridview double click is not showing showing output in richtext form.How to create a password system for deletions using a button, RRS feed

  • Question

  • Hie good day

    I have slight problem whereby i need to double click my gridview so that the output can show in the richtext form.The other issue i would like assistance on how to create a user password for admins before deletions.That is only admins can enter their password for deletions.Below is the sample code

    Regards

    mcfee

     public partial class Activities : Form
        {
            public Activities()
            {
                InitializeComponent();
            }
            //variables declared
            private SQLiteConnection sqlconn;
            private SQLiteCommand sqlCmd;
            private SQLiteDataAdapter DB;
            private DataSet DS = new DataSet();
            private DataTable sqlDT = new DataTable();
    
            private void Activities_Load(object sender, EventArgs e)
            {
                LoadData();
              
            }
    
         
           
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
            //set connection 
            private void SetConnection()
            {
    
                sqlconn = new SQLiteConnection("Data Source = D:\\WeaponInventoryTracker\\WeaponInventoryTracker\\WIT.db;");
    
            }
            //set execute query 
            private void ExecuteQuery(string txtQuery)
            {
    
                SetConnection();
                sqlconn.Open();
                sqlCmd = sqlconn.CreateCommand();
                sqlCmd.CommandText = txtQuery;
                sqlCmd.ExecuteNonQuery();
               //sqlconn.Dispose();
                sqlconn.Close();
    
            }
            //set loadDB method to call database
            private void LoadData()
            {
                SetConnection();
                sqlconn.Open();
                sqlCmd = sqlconn.CreateCommand();
                string CommandText = "select *  from info ";
                DB = new SQLiteDataAdapter(CommandText, sqlconn);
                DS.Reset();
                DB.Fill(DS);
                sqlDT = DS.Tables[0];
                dataGridView1.DataSource = sqlDT;
                sqlconn.Close();
            }
    
            private void label9_Click(object sender, EventArgs e)
            {
    
            }
    
            private void textBoxAmmunNo_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void buttonExit_Click(object sender, EventArgs e)
            {
                DialogResult iExit;
                iExit = MessageBox.Show("Are you sure you want to exit?", "Activities", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (iExit == DialogResult.Yes)
                {
                    this.Close();
                }
            }
    
            void Cleartext(Control con)
            {
                foreach (Control c in con.Controls)
                {
                    if (c is TextBox)
                        ((TextBox)c).Clear();
    
                    else
                        Cleartext(c);
                }
    
            }
            private void buttonNewReset_Click(object sender, EventArgs e)
            {
                Cleartext(this);
               
                rtOutputInfo.Text = "";
            }
    
            private void NumberOnly(object sender, KeyPressEventArgs e)
            {
                int asciicode = Convert.ToInt32(e.KeyChar);
                if (asciicode != 8)
                {
                    if (asciicode >= 48 && asciicode <= 57)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        MessageBox.Show("Enter Number Only!", "Error:NumberOnly", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Handled = true;
                    }
                }
            }
    
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
                //OUT
                textBoxTimout.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                textBoxDateOut.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                textBoxRecForceNo.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                textBoxRecName.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                textBoxRecSig.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
               
         
                
                //WEAPON
                textBoxSerialNo.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                textBoxAmmunAmt.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                textBoxAmmunNo.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
                textBoxFireArmSum.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
                textBoxFireAType.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
                textBoxWeapType.Text = dataGridView1.SelectedRows[0].Cells[10].Value.ToString();
                textBoxIssuerFoNo.Text = dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
                textBoxIssuerName.Text = dataGridView1.SelectedRows[0].Cells[12].Value.ToString();
                textBoxIssSig.Text = dataGridView1.SelectedRows[0].Cells[13].Value.ToString();
    
                //IN
                textBoxRetForceNumber.Text = dataGridView1.SelectedRows[0].Cells[14].Value.ToString();
                textBoxRetName.Text = dataGridView1.SelectedRows[0].Cells[15].Value.ToString();
                textBoxRetSig.Text = dataGridView1.SelectedRows[0].Cells[16].Value.ToString();
                textBoxDateIn.Text = dataGridView1.SelectedRows[0].Cells[17].Value.ToString();
                textBoxTimeIn.Text = dataGridView1.SelectedRows[0].Cells[18].Value.ToString();  
                
               //OTHER
                textBoxSecOfficer.Text = dataGridView1.SelectedRows[0].Cells[19].Value.ToString();
                textBoxManager.Text = dataGridView1.SelectedRows[0].Cells[20].Value.ToString();
                richTextBoxComments.Text = dataGridView1.SelectedRows[0].Cells[21].Value.ToString();
                
            }
    
            private void textBoxWeapType_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void buttonViewInfo_Click(object sender, EventArgs e)
            {
                rtOutputInfo.AppendText("SERIALNO :\t\t\t\t" + textBoxSerialNo.Text +  "\n");
                rtOutputInfo.AppendText("DATEOUT :\t\t\t\t" + textBoxDateOut.Text + "\n");
                rtOutputInfo.AppendText("TIMEOUT :\t\t\t\t" + textBoxTimout.Text + "\n");
                rtOutputInfo.AppendText("FIREARMTYPE :\t\t\t\t" + textBoxFireAType.Text + "\n");
                rtOutputInfo.AppendText("FIREARMSUM :\t\t\t\t" + textBoxFireArmSum.Text + "\n");
                rtOutputInfo.AppendText("AMMUNITIONNO :\t\t\t\t" + textBoxAmmunNo.Text + "\n");
                rtOutputInfo.AppendText("ISSUERFORCENO :\t\t\t\t" + textBoxIssuerFoNo.Text + "\n");
                rtOutputInfo.AppendText("ISSUERNAME :\t\t\t\t" + textBoxIssuerName.Text + "\n");
                rtOutputInfo.AppendText("ISSUERSIG : \t\t\t\t" + textBoxIssSig.Text + "\n");
                rtOutputInfo.AppendText("RECIEVERFORCENO :\t\t\t\t" + textBoxRecForceNo.Text + "\n");
                rtOutputInfo.AppendText("RECIEVERNAME :\t\t\t\t" + textBoxRecName.Text + "\n");
                rtOutputInfo.AppendText("RECIEVERSIG :\t\t\t\t" + textBoxRecSig.Text + "\n");
                rtOutputInfo.AppendText("TIMEIN :\t\t\t\t" + textBoxTimeIn.Text + "\n");
                rtOutputInfo.AppendText("DATEIN :\t\t\t\t" + textBoxDateIn.Text + "\n");
                rtOutputInfo.AppendText("WEAPONTYPE :\t\t\t\t" + textBoxWeapType.Text + "\n");
                rtOutputInfo.AppendText("AMMUNITIONAMT :\t\t\t\t" + textBoxAmmunAmt.Text + "\n");
                rtOutputInfo.AppendText("RETURNERFORCENO :\t\t\t\t" + textBoxRetForceNumber.Text + "\n");
                rtOutputInfo.AppendText("RETURNERNAME :\t\t\t\t" + textBoxRetName.Text + "\n");
                rtOutputInfo.AppendText("RETURNERSIGNATURE :\t\t\t\t" + textBoxRetSig.Text + "\n");
                rtOutputInfo.AppendText("MANAGER :\t\t\t\t" + textBoxManager.Text + "\n");
                rtOutputInfo.AppendText("SECURITYOFFICER :\t\t\t\t" + textBoxSecOfficer.Text + "\n");
                rtOutputInfo.AppendText("COMMENTS :\t\t\t\t" + richTextBoxComments.Text + "\n");
                
            }
    
            private void buttonUpdate_Click(object sender, EventArgs e)
            {                                          //colums
                string txtQuery = "insert into info (DATEOUT,TIMEOUT,FIREARMTYPE,FIREARMSUM	,AMMUNITIONNO,ISSUERFORCENO	,ISSUERNAME,	ISSUERSIG ,RECIEVERFORCENO,	RECIEVERNAME,	RECIEVERSIG	,TIMEIN	,DATEIN,	WEAPONTYPE,	SERIALNO	,AMMUNITIONAMT,	RETURNERFORCENO,	RETURNERNAME,	RETURNERSIGNATURE	,COMMENTS	,MANAGER	,SECURITYOFFICER	) values('"+ textBoxDateOut.Text + "','" + textBoxTimout.Text + "','" + textBoxFireAType.Text + "','" + textBoxFireArmSum.Text + "','" + textBoxAmmunNo.Text + "','" + textBoxIssuerFoNo.Text + "','" + textBoxIssuerName.Text + "','" + textBoxIssSig.Text + "','" + textBoxRecForceNo.Text + "','" + textBoxRecName.Text + "','" + textBoxRecSig.Text + "' , '" + textBoxTimeIn.Text + "','" + textBoxDateIn.Text + "','" + textBoxWeapType.Text + "','" + textBoxSerialNo.Text + "','" + textBoxAmmunAmt.Text + "','" + textBoxRetForceNumber.Text+ "','"+ textBoxRetName.Text + "','" + textBoxRetSig.Text + "','" + textBoxManager.Text + "','" + textBoxSecOfficer.Text + "','" + richTextBoxComments.Text + "' )";
                
                ExecuteQuery(txtQuery);
                LoadData();
            }
    
            private void buttonDelete_Click(object sender, EventArgs e)
            {
               // string txtQuery = "delete from info where SERIALNO = serialno " + textBoxDateOut.Text + textBoxTimout.Text + textBoxFireAType.Text + textBoxFireArmSum.Text + textBoxAmmunNo.Text  + textBoxIssuerFoNo.Text  + textBoxIssuerName.Text  + textBoxIssSig.Text  + textBoxRecForceNo.Text  + textBoxRecName.Text + textBoxRecSig.Text + textBoxTimeIn.Text  + textBoxDateIn.Text + textBoxWeapType.Text + textBoxSerialNo.Text  + textBoxAmmunAmt.Text  + textBoxRetForceNumber.Text  + textBoxRetName.Text  + textBoxRetSig.Text + textBoxManager.Text  + textBoxSecOfficer.Text + richTextBoxComments.Text ;
               //ExecuteQuery(txtQuery);
               // LoadData();
               
    }  
    
            //declare object code
            Bitmap bitmap;
            private void buttonPrintActivityInfo_Click(object sender, EventArgs e)
            {
                int height = dataGridView1.Height;
                dataGridView1.Height = dataGridView1.RowCount * dataGridView1.RowTemplate.Height * 2;
                bitmap = new Bitmap(dataGridView1.Width, dataGridView1.Height);
                dataGridView1.DrawToBitmap(bitmap, new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height));
                printPreviewDialog1.PrintPreviewControl.Zoom = 1;
                printPreviewDialog1.ShowDialog();
                dataGridView1.Height = height;
    
            }
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                DialogResult iPrint;
                iPrint = MessageBox.Show("Confirm Print Current Inventory Info or all Info?","Inventory Information",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
    
    
                if (iPrint == DialogResult.Yes)
                {
    
                    e.Graphics.DrawString(rtOutputInfo.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(0, 0));
    
                }
                else
                {
                    e.Graphics.DrawImage(bitmap, 0, 0); 
                }
            }
    
            private void printPreviewDialog1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
    
            }
    
            private void buttonOverdue_Click(object sender, EventArgs e)
            {
                if (textBoxTimout.Text == "")
    
                    textBoxTimeIn.Text = "";
    
                int[] R = new int[15];
                R[0] = Convert.ToInt32(textBoxTimout.Text);
                R[1] = Convert.ToInt32(textBoxTimeIn.Text);
                R[2] =  (R[0] - R[1]);
                textBoxTimeOverdue.Text = Convert.ToString(R[2]);
                if  (R[2] > 12)
                {
    
                    textBoxTimeOverdue.Text = ("On time"); 
                }
    
                else if ( R[2] < 12)
                {
                    textBoxTimeOverdue.Text = ("Overdue Submission");
                }
                
            }
    
    
            private void textBoxTimeOverdue_TextChanged(object sender, EventArgs e)
            {
                
            }
    
          
    
           
          
             
            
        }
    }


    Saturday, August 17, 2019 11:50 PM

All replies

  • Hi mcfee12345,

    Thank you for posting here.

    First, you could use the following code to show text in richtextbox by double clicking the datagridview.

     private void DataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                var m = e.ColumnIndex;
                var n = e.RowIndex;
                var result = dataGridView1.Rows[n].Cells[m].Value.ToString();
                richTextBox1.Text = result;
            }
    

    Second, you could create a new table to set permissions to delete table.

    private void Form1_Load(object sender, EventArgs e)
    {
       createnewtable();
    
    }
    private void createnewtable()
            {
                SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
                m_dbConnection.Open();
                string sql = "create table User (Name varchar(20), Password int)";
                SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
                sql = "insert into User (Name, Password) values('TestUser',123456)";
                command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
            }
         private void Btndeleted_Click(object sender, EventArgs e)
            {
                if(txtPassword.Text==string.Empty||txtUser.Text==string.Empty)
                {
                    MessageBox.Show("Please input username or password to delete");
                }
                else
                {
                    string username = txtUser.Text;
                    string password = txtPassword.Text;
                    string sql = string.Format("select * from User where Name='{0}' AND Password='{1}'", username, password);
                    SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
                    m_dbConnection.Open();
                    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                    int i=command.ExecuteNonQuery();
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, m_dbConnection);
                    DataSet set = new DataSet();
                    adapter.Fill(set);
                    DataTable table = set.Tables[0];
                    if (table.Rows.Count>0)
                    {
                        //Please delete record according to the conditions
                        MessageBox.Show("success");
                    }
                    else
                    {
                        MessageBox.Show("please input correct username and password");
                    }
    
    
                }
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 23, 2019 7:07 AM
    Moderator