0

我有 1 个网格视图。单击网格视图单元格时,它将在另一个中显示相关数据gridview

现在另一个gridview有了DataGridViewComboBoxColumn

它的数据正在加载,但在加载时显示空白。当我点击它时它正在显示DataGridViewComboBoxColumn

我想要的是显示数据而不是空白。

这将填充"grdAgingInvoice" gridview其他列

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());
  }
}

这将添加 DataGridViewComboBoxColumn"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;
  }
}
4

1 回答 1

0

您可以使用NullValue属性DataGridViewComboBoxColumn.DefaultCellStyle来设置默认的组合框值:

int columnIndex = Your ComboBoxColumn index;
(dataGridView1.Columns[columnIndex] as DataGridViewComboBoxColumn).DefaultCellStyle.NullValue = "Nothing selected";

在此处输入图像描述

于 2021-10-13T08:38:45.690 回答