datagridview problem
Hi,
I am developing a windows application, my database is in MS-Access, i have made one form which have 2 combobox. 1 will take the license key from tblantivirus and the 2nd one will take the employee name from tblemployee. The table for this form is tblemployeelicense. I have done the coding so that at the backend it will store employeeId and licenseId. But i want that in the form's datagridview licensekey and employeename will be displayed instead of employeeID and licenseId. But at the backend it will store the ids only. Please help me out with this.
My coding is:
public frmemplicense()
{
InitializeComponent();
}
string employeeName;
string licenseKey;
public string pageAction;
private void frmemplicense_Load(object sender, EventArgs e)
{
LoadGrid();
SetEditState(false);
ClearFields();
LoadData();
}
private void LoadData()
{
try
{
ConnectionClass l_ConnectionClass = new ConnectionClass();
DataSet KeyData = l_ConnectionClass.FetchDataInDataSet("select licenseId,licenseKey from tblantivirus");
cblicense.ValueMember = "licenseId";
cblicense.DisplayMember = "licenseKey";
cblicense.DataSource = KeyData.Tables[0];
DataSet EmployeeData = l_ConnectionClass.FetchDataInDataSet("select employeeId,employeeName from tblemployee");
cbemployee.ValueMember = "employeeId";
cbemployee.DisplayMember = "employeeName";
cbemployee.DataSource = EmployeeData.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ClearFields()
{
cbemployee.Text = String.Empty;
cblicense.Text = String.Empty;
}
private void SetEditState(bool edit)
{
//when a record is selected for edit, disable the Add,Modify & Close Buttons
btnadd.Enabled = !edit;
//btnmodify.Enabled = !edit;
btnclose.Enabled = !edit;
//btndelete.Enabled = !edit;
//when we are editing, do nat allow to navigate in the grid
dgvemplicense.Enabled = !edit;
//when we are editing, only Cancel and Save buttons are enabled
btncancel.Enabled = edit;
btnsave.Enabled = edit;
cbemployee.Enabled = edit;
cblicense.Enabled = edit;
}
private void LoadGrid()
{
try
{
ConnectionClass l_ConnectionClass = new ConnectionClass();
DataSet l_employeelicense = l_ConnectionClass.FetchDataInDataSet("select * from tblemployeelicense");
dgvemplicense.DataSource = l_employeelicense.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnadd_Click(object sender, EventArgs e)
{
pageAction = "ADD";
ClearFields();
SetEditState(true);
cblicense.Focus();
}
private void btnsave_Click(object sender, EventArgs e)
{
try
{
ConnectionClass l_ConnectionClass = new ConnectionClass();
//licenseId = l_ConnectionClass.generateID("select licenseId from tblantivirus", "licenseId");
//employeeId = l_ConnectionClass.generateID("select employeeId from tblemployee", "employeeId");
licenseKey = Convert.ToString(cblicense.SelectedValue);
employeeName = Convert.ToString(cbemployee.SelectedValue);
int checkreturn = 0;
int checkemplicence = 0;
checkreturn = Convert.ToInt32(l_ConnectionClass.CheckLicense(licenseKey));
checkemplicence = Convert.ToInt32(l_ConnectionClass.CheckEmployeeLicense(employeeName));
if (checkreturn >= 3)
{
MessageBox.Show("Number of users exceeded for this license key");
LoadGrid();
ClearFields();
SetEditState(false);
}
else
{
if (checkemplicence >= 1)
{
MessageBox.Show("this employee has already license key");
LoadGrid();
ClearFields();
SetEditState(false);
}
else
{
//MessageBox.Show(checkreturn.ToString());
l_ConnectionClass.SaveEmployeeLicense(licenseKey, employeeName);
LoadGrid();
ClearFields();
cblicense.Focus();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btncancel_Click(object sender, EventArgs e)
{
LoadGrid();
ClearFields();
SetEditState(false);
}
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void dgvemplicense_SelectionChanged(object sender, EventArgs e)
{
try
{
if (dgvemplicense.SelectedRows.Count > 0)
{
cblicense.SelectedValue = dgvemplicense.CurrentRow.Cells["licenseKey"].Value.ToString();
cbemployee.SelectedValue = dgvemplicense.CurrentRow.Cells["employeeName"].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Please help me with the codes. I am new to programming. Please.
naura paxPosted Oct 28, 2009, 5:56 AM
Pardon me I couldn't go through entire code, but as i could understand your problem i have thought of following solution:
1. You fill your employee combobox with ValueMember as EmploeeID and Display Member as Employee Name same for Licence combo.
2.Add EmployeeName & LicenseNo column into your datagridview along with ID columns and their datapropertyname set to same.
3.When you fill the datagridview you run following query:
"Select EmployeeID,tblemployeeMaster.EmployeeName,LicenseID,LicenseNo from tblemployeeMaster inner join tblTransaction (table for gridview) on tblemployeeMaster.EmployeeID=tblTransaction.EmployeeID inner join
tblLicense on tblLicense.LicenseID=tblTransaction.LicenseID
so therey by with join in the query to fill datagridview ( or its dataset) you can achieve results.