A.
Coding Program
Variabel
Komponen
|
Source Code
|
btnExportToExcel
|
private void
btnExportToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new
SaveFileDialog();
sfd.Filter = "Excel
Documents (*.xls)|*.xls";
sfd.FileName = "Registrasi
Kehadiran Rapat.xls";
if (sfd.ShowDialog() ==
DialogResult.OK)
{
//ToCsV(dataGridView1,
@"c:\export.xls");
ToCsV(dataGridView1,
sfd.FileName); // Here dataGridview1 is your grid view name
}
}
private void ToCsV(DataGridView dGV, string
filename)
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j <
dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString()
+ Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders +
"\r\n";
// Export data.
for (int i = 0; i <
dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 0; j <
dGV.Rows[i].Cells.Count; j++)
stLine =
stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) +
"\t";
stOutput += stLine +
"\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output =
utf16.GetBytes(stOutput);
FileStream fs = new
FileStream(filename, FileMode.Create);
BinaryWriter bw = new
BinaryWriter(fs);
bw.Write(output, 0,
output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
|
btnInputData
|
private void btnInputData_Click(object sender, EventArgs e)
{
string[] row = new string[]
{
txtNama.Text, txtNIM.Text,
txtJabatan.Text, txtWaktu.Text
};
dataGridView1.Rows.Add(row);
}
|
btnDeleteDataSelected
|
private void btnDeleteDataSelected_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell oneCell in
dataGridView1.SelectedCells)
{
if (oneCell.Selected)
dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
}
}
|
btnExit
|
private void btnExit_Click(object sender,
EventArgs e)
{
DialogResult result;
string judul =
"Confirmation";
string pesan = "Apakah Anda
yakin ingin keluar?";
MessageBoxButtons pil_tombol =
MessageBoxButtons.YesNo;
result = MessageBox.Show(pesan,
judul, pil_tombol, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Application.Exit();
}
}
|
Posting Komentar