Reputation: 1
I have 1 grid view. On grid-view cell click, it will show relevant data in another gridview
.
Now another gridview
has DataGridViewComboBoxColumn
.
Its data is loading but showing blank on load .
It is showing when i click on that DataGridViewComboBoxColumn
.
What i want is shows data instead of blank.
This will fill "grdAgingInvoice"
gridview
other columns
public void grdAgingeInvoice(int RowIndex) {
DataTable DTCombo = new DataTable();
try {
var AgingeInvoice = new DataTable();
var tEMP = new DataTable();
string Query;
grdAgingInvoice.DataSource = null;
grdAgingInvoice.Columns.Clear();
Query = "Select Aging, DueInvoiceNo, DueDate, Balance,CompanyID from AgingInvoice where CompanyID=" + grdCompany.Rows[RowIndex].Cells["CompanyID"].Value.ToString() + " Order by Aging Desc";
AgingeInvoice = StMethod.GetListDT<DueInvoice>(Query);
AgingeInvoice.Columns.Add("ContactsName");
AgingeInvoice.Columns.Add("EmailAddress");
AgingeInvoice.Columns.Add("Summation");
AgingeInvoice.Columns.Add("Address");
AgingeInvoice.Columns.Add("Description");
var Summation = default(decimal);
foreach (DataRow dr in AgingeInvoice.Rows) {
Summation = Summation + Convert.ToDecimal(dr["Balance"]);
dr["Summation"] = Summation;
string StrAddress = "SELECT ( Contacts.FirstName+' '+Contacts.LastName) AS Contact, Contacts.EmailAddress,Joblist.Address,JobList.Description FROM JobList INNER JOIN Contacts ON JobList.ContactsID = Contacts.ContactsID WHERE JobList.JobNumber= '" + Program.GetJobNumber(dr["DueInvoiceNo"].ToString()) + "'";
tEMP = StMethod.GetListDT<InvoiceAddress>(StrAddress);
if (tEMP.Rows.Count > 0) {
string s1;
s1 = tEMP.Rows[0]["Contact"].ToString();
dr["ContactsName"] = tEMP.Rows[0]["Contact"].ToString();
dr["EmailAddress"] = tEMP.Rows[0]["EmailAddress"].ToString();
dr["Address"] = tEMP.Rows[0]["Address"].ToString();
dr["Description"] = tEMP.Rows[0]["Description"].ToString();
}
}
grdAgingInvoice.DataSource = AgingeInvoice;
string Query2 = string.Empty;
String query3 = "SELECT (FirstName +' '+ LastName) AS Contact,";
Query2 = query3 + "EmailAddress,ContactsID ";
Query2 = Query2 + "FROM contacts ";
Query2 = Query2 + "WHERE (IsDelete=0 or IsDelete IS NULL) AND Companyid=";
Query2 = Query2 + grdCompany.Rows[grdCompany.CurrentRow.Index].Cells["CompanyID"].Value.ToString();
grdAgingInvoice.Columns.Insert(5, AddContactCombo());
{
var withBlock = grdAgingInvoice;
withBlock.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
withBlock.Columns["CompanyID"].Visible = true;
withBlock.Columns["DueInvoiceNo"].HeaderText = "Due InvoiceNo";
withBlock.Columns["DueDate"].HeaderText = "Due Date";
// .Columns["ContactsName"].HeaderText = "Contacts Name" 'Add to new column In grid view
withBlock.Columns["EmailAddress"].HeaderText = "Email Address"; // Add to new column In grid view
withBlock.Columns["Description"].HeaderText = "Job Description";
withBlock.Columns["ContactsName"].Visible = true;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message.ToString());
}
}
This will add DataGridViewComboBoxColumn
to "grdAgingInvoice"
gridview
.
public DataGridViewComboBoxColumn AddContactCombo() {
DataGridViewComboBoxColumn ComboBoxContactsName = new DataGridViewComboBoxColumn();
try {
DataTable dt2 = new DataTable();
dt2 = StMethod.GetListDT<InvoiceAddress>("SELECT (Contacts.FirstName +' '+ Contacts.LastName) as Contact,Contacts.EmailAddress,ContactsID FROM contacts where (Contacts.IsDelete=0 or Contacts.IsDelete IS NULL) AND Contacts.Companyid=" + grdCompany.Rows[grdCompany.CurrentRow.Index].Cells["CompanyID"].Value.ToString());
ComboBoxContactsName.DataSource = dt2;
ComboBoxContactsName.DisplayMember = dt2.Columns["Contact"].ToString();
ComboBoxContactsName.ValueMember = dt2.Columns["ContactsID"].ToString();
ComboBoxContactsName.DisplayIndex = 5;
ComboBoxContactsName.HeaderText = "Contacts Name";
ComboBoxContactsName.DataPropertyName = dt2.Columns["Contact"].ToString();
ComboBoxContactsName.Name = "cmbContactsName";
ComboBoxContactsName.DefaultCellStyle.NullValue = dt2.Columns["Contact"];
return ComboBoxContactsName;
}
catch (Exception ex) {
return ComboBoxContactsName;
}
}
Upvotes: 0
Views: 106
Reputation: 2049
You can use NullValue
property of DataGridViewComboBoxColumn.DefaultCellStyle
to set a default ComboBox value:
int columnIndex = Your ComboBoxColumn index;
(dataGridView1.Columns[columnIndex] as DataGridViewComboBoxColumn).DefaultCellStyle.NullValue = "Nothing selected";
Upvotes: 0