如何使用c#winforms在绑定的DataGridView中显示未绑定的列

时间:2016-01-13 10:44:22

标签: c# winforms datagridview

我有一个DataGridView,我正在填充如下所示。这个DataGridView有7列,其中1st,4th&第6列分别是未绑定的,其余的是绑定列。现在我的要求是

  

1]在[0]列中,我想显示一个自动增量整数值,如“序列号” 1,2,3 .....Ñ

     

2]在第[4]栏,我想显示图像列表中的图标/图像。

     

3]在第[6]栏,我想显示一个Button控件

      try
        {
            using (FbConnection conLLV = new FbConnection(connectionString))
            {
                conLLV.Open();
                using (FbCommand cmdLLV = new FbCommand(sqlQryLLV, conLLV))
                {
                    cmdLLV.Parameters.Add("@t_id", FbDbType.Integer).Value = tid;
                    cmdLLV.Parameters.Add("@mem_id", FbDbType.Integer).Value = mid;
                    cmdLLV.CommandType = CommandType.Text;
                    using (FbDataAdapter daLLV = new FbDataAdapter(cmdLLV))
                    {
                        using (DataTable dtLLV = new DataTable())
                        {
                            daLLV.Fill(dtLLV);

                            dgSSW.AutoGenerateColumns = false;
                            dgSSW.ColumnCount = 7;

                            //At Column[0] ->a Serial No column

                            dgSSW.Columns[1].Name = "subsec_name";
                            dgSSW.Columns[1].HeaderText = "Sub Section Name";
                            dgSSW.Columns[1].DataPropertyName = "subsec_name";

                            dgSSW.Columns[2].Name = "rt_correct_ans";
                            dgSSW.Columns[2].HeaderText = "Correct Answer";
                            dgSSW.Columns[2].DataPropertyName = "rt_correct_ans";

                            dgSSW.Columns[3].Name = "rt_your_ans";
                            dgSSW.Columns[3].HeaderText = "Your Answer";
                            dgSSW.Columns[3].DataPropertyName = "rt_your_ans";

                            //At Column[4] ->an Image column

                            dgSSW.Columns[5].Name = "q_r_difficulty";
                            dgSSW.Columns[5].HeaderText = "Difficulty Level";
                            dgSSW.Columns[5].DataPropertyName = "q_r_difficulty";

                            //At Column[6] ->a column having a Button control

                            dgSSW.DataSource = dtLLV;
                        }//data table closed and disposed here
                    }// data adapter closed and disposed up here
                 }// command disposed here
            }//connection closed and disposed here
        } 
        catch (FbException ex)
        {
            MessageBox.Show("LLV--" + ex.Message);
        }

我如何达到我的要求请告知使用代码。我搜索了互联网,但所提供的解决方案与我的要求不符。

1 个答案:

答案 0 :(得分:1)

可能的解决方案:

0]设置数据源

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "script": {
                "script": "_source.stream_id.length() == 0"
              }
            },
            {
              "term": {
                "source_id": "unknown_source"
              }
            }
          ]
        }
      }
    }
  }
}

1]行号:为它们添加列并应用单元格格式

{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": "_source.stream_id.length() == 0"
          }
        },
        {
          "term": {
            "source_id": "unknown_source"
          }
        }
      ]
    }
  }
}

2]图片栏:

dgSSW.DataSource = dtLLV;

3]按钮列,点击处理

var col0 = new DataGridViewTextBoxColumn
    {
        HeaderText = "#", Name="RowNum", 
        ReadOnly = true, 
        Width = 10
    };
dgSSW.Columns.Insert(0, col0);

dgSSW.CellFormatting += GridCellFormatting;

private void GridCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dgSSW.Columns[e.ColumnIndex].Name == "RowNum")
    {
        e.Value = (e.RowIndex + 1).ToString();
    }
}

更新:由于您未使用var col3 = new DataGridViewImageColumn { HeaderText = "Pic", Name = "Pic" }; dgSSW.Columns.Insert(4, col3); // set image for a cell dgSSW["Pic", 0].Value = Resources.add; 并手动创建所有列,因此您可以创建所有col0,col3,col7以及之后设置var col7 = new DataGridViewButtonColumn { HeaderText = "Proceed", Name = "Action", Text = "+", UseColumnTextForButtonValue = true }; dgSSW.Columns.Add(col7); dgSSW.CellContentClick += GridCellContentClick; private void GridCellContentClick(object sender, DataGridViewCellEventArgs e) { if (dgSSW.Columns[e.ColumnIndex].Name == "Action") { MessageBox.Show((e.RowIndex + 1).ToString()); } }