DataGridView DataSource C# Source Code
How to fill DataGridView items on c# if we're connect using OleDbConnection?
We're gonna use DataSet for filling the DataGridView.. Here'is the example...
The information about variables and objects used is
- dataGridView1 --> DataGridView GUI Object from Toolbox
- Program.con --> OleDbConnection (using System.Data.OleDb)
- combo_produk_id --> ComboBox GUI Object from Toolbox
private void Item_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
DataSet ds = new DataSet();
Program.con.Open();
OleDbCommand cmd = Program.con.CreateCommand();
cmd.CommandText = "SELECT * FROM produk";
OleDbDataReader reader = cmd.ExecuteReader();
combo_produk_id.Items.Clear();
while (reader.Read())
{
combo_produk_id.Items.Add(reader.GetValue(0));
}
Program.con.Close();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
Greedy Activity Selector C Source Code
The details of this problem can be find on my previous post : Greedy Activity Selector Java Source Code
First we must declare the function prototype
void recursiveActivitySelector(int*, int*, int, int, int*); void greedyActivitySelector(int*, int*, int, int*);
Then here is the Recursive Solution of Greedy Activity Selector
void recursiveActivitySelector(int s[], int f[], int i, int j, int a[]){
int m = i + 1;
while (m < j && s[m] < f[i]){
m = m + 1;
}
if (m < j){
a[m] = 1;
recursiveActivitySelector(s,f,m,j,a);
}
}
And the Iterative Solution is
void greedyActivitySelector(int s[], int f[], int len, int a[]){
int n = len;
int i = 1;
for (int m=1;m<n;m++){
if (s[m] >= f[i]){
a[m] = 1;
i = m;
}
}
}
For testing the program, you can use main function like this
int main(){
int a1[] = {1,0,0,0,0,0,0,0,0,0,0};
int a2[] = {1,0,0,0,0,0,0,0,0,0,0};
int s[] = {1,3,0,5,3,5,6,8,8,2,12};
int f[] = {4,5,6,7,8,9,10,11,12,13,14};
int len = sizeof(s) / sizeof(int);
recursiveActivitySelector(s, f, 0, len, a1);
printf("Recursive Solution : ");
for (int i=0;i<len;i++){
if (a1[i] == 1)
printf("%d ",i+1);
}
printf("\n");
greedyActivitySelector(s, f, len, a2);
printf("Iterative Solution : ");
for (int i=0;i<len;i++){
if (a2[i] == 1)
printf("%d ",i+1);
}
getch();
}
Greedy Activity Selector Java Source Code
Activity Selector is problem to choose which event will be choosen to get maximum number of event for using the resources (time). Each event has start time and finish time. Two or more event cannot use the resource at the same time.
And here is the recursive and iterative solution pseudocode based on Introduction to Algorithm, 2nd edition
RECURSIVE-ACTIVITY-SELECTOR(s, f, i, j)
1 m ← i + 1
2 while m < j and sm < fi ▹ Find the first activity in Sij.
3 do m ← m + 1
4 if m < j
5 then return {am} ∪ RECURSIVE-ACTIVITY-SELECTOR(s, f, m, j)
6 else return Ø
GREEDY-ACTIVITY-SELECTOR(s, f)
1 n ← length[s]
2 A ← {a1}
3 i ← 1
4 for m ← 2 to n
5 do if sm ≥ fi
6 then A ← A ∪ {am}
7 i ← m
8 return A
How about the implementation on java source code.... Let's check this out
public static String recursiveActivitySelector(int[] s, int[] f, int i, int j){
int m = i + 1;
while (m < j && s[m] < f[i]){
m = m + 1;
}
if (m < j){
return Integer.toString(m+1) +" "+ recursiveActivitySelector(s, f, m, j);
}
else return "";
}
And the iterative solution is....
public static String iterativeActivitySelector(int[] s, int[] f){
String a = "1";
int i = 0;
int n = s.length;
for (int m=1;m<n;m++){
if (s[m] >= f[i]){
a = a + " " + (m+1);
i = m;
}
}
return a;
}
Okay, that's it... Try and keep going with your work
Java ResultSet to JTable
How to Show Java ResultSet to JTable?
First, let me write some requirements for this post
- mysql-connector-java-5.1.x.jar (as a library for connecting mysql dan java)
- Some import
import java.sql.ResultSet; import javax.swing.table.DefaultTableModel;
And here is the method
public static DefaultTableModel ResultSet2TableModel(ResultSet rs){
try{
DefaultTableModel dtm = new DefaultTableModel();
ResultSetMetaData meta = rs.getMetaData();
int col = meta.getColumnCount();
rs.last();
int row = rs.getRow();
rs.beforeFirst();
Object[] rowData = new Object[col];
Object[] colData = new Object[col];
for (int i=0;i<col;i++){
colData[i] = meta.getColumnLabel(i+1);
}
dtm.setColumnIdentifiers(colData);
for (int i=0;i<row;i++){
rs.next();
for (int j=0;j<col;j++){
rowData[j] = rs.getString(j+1);
}
dtm.addRow(rowData);
}
return dtm;
} catch (Exception ex){
JOptionPane.showMessageDialog(null, "ResultSet to TableModel Converter : "+ex.getMessage());
return null;
}
}
The return value is DefaultTableModel, you can use it like
ResultSet rs = con.executeQuery(sql); DefaultTableModel dtm = ResultSet2TableModel(rs); myJTable.setModel(dtm);
Note : I'm Using
- NetBeans IDE 6.7
- JDK 7
- mysql-connector-java-5.1.6.jar
- Windows XP SP3 Operating System




