如何在datagridview中填充数组?

时间:2017-12-23 12:12:57

标签: c# mysql

我创建了一个带有两个输入参数和一个输出参数的存储过程。它返回一个包含12个项目列表的字符串,如"(46,46,46,46,47,48,49,90,90,94,98,25)"

现在在C#代码中,我将该数组分成一个数组变量:

string[] valuesm = MON.Split(',');

for (int i = 1; i < valuesm.Length; i++)
{valuesm[i] = valuesm[i].Trim();}

现在我想在像

这样的网格视图中显示它
image | 46|46|46|46|47|48|49|90|90|94|98|25 | something here

我该怎么做?

其余代码如下:

public void GridPopulateMaleView()
{
    string constr = ConfigurationManager.ConnectionStrings["dataconnection"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand GRID_HEADER_CMD = new MySqlCommand("SELECT SUM(Tscore) as TSCORE, Celeb_name, celeb_code FROM `actor_tscore_july_17` group by celeb_name order by TSCORE desc;", con))
        {
            {
                using (MySqlDataAdapter GRID_HEADER_sda = new MySqlDataAdapter(GRID_HEADER_CMD))
                {
                    GRID_HEADER_dt = new DataTable();
                    GRID_ACTOR_DT = new DataTable();
                    GRID_HEADER_sda.Fill(GRID_HEADER_dt);
                    if (GRID_HEADER_dt != null)
                    {
                        if (GRID_HEADER_dt.Rows.Count > 0)
                        {
                            foreach (DataRow row in GRID_HEADER_dt.Rows)
                            {
                                Celeb_Male_Name_TW = row["celeb_name"].ToString();
                                Celeb_Male_Code_TW = row["celeb_code"].ToString();
                                using (MySqlCommand Celeb_Male_Update = new MySqlCommand("SP_TWELVEMONTHDATAACTOR12", con))
                                {
                                    Celeb_Male_Update.CommandType = CommandType.StoredProcedure;
                                    Celeb_Male_Update.Parameters.AddWithValue("@CELEB_NAME_TW", Celeb_Male_Name_TW);
                                    Celeb_Male_Update.Parameters.AddWithValue("@CELEB_CODE", Celeb_Male_Code_TW);
                                    Celeb_Male_Update.Parameters.Add("@SCORE_MONTH", MySqlDbType.VarChar, 255);
                                    Celeb_Male_Update.Parameters["@SCORE_MONTH"].Direction = ParameterDirection.Output;
                                    con.Open();
                                    Celeb_Male_Update.ExecuteNonQuery();
                                    string MON = Celeb_Male_Update.Parameters["@SCORE_MONTH"].Value.ToString(); //string[] values = MON.Split(',').Select(sValue => sValue.Trim()).ToArray();
                                    //string[] valuesm = new string[12];
                                    string[] valuesm = MON.Split(',');
                                    //for (int i = 1; i < valuesm.Length; i++)
                                    {
                                        valuesm[i] = valuesm[i].Trim();
                                        //valuesm[i] = new string[12] { "Value 1", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2", "Value 2" };
                                    }

                                    gridActorTScore.DataSource = (from arr in valuesm select new { Col2 = arr[1], Col3 = arr[2], Col4 = arr[3], Col5 = arr[4], Col6 = arr[5], Col7 = arr[6], Col8 = arr[7], Col9 = arr[8], Col10 = arr[9], Col11 = arr[10], Col12 = arr[11], Col13 = arr[12]});
                                    //gridActorTScore.DataSource = dt;
                                    gridActorTScore.DataBind();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码很难阅读,但我建议尝试将其视为属性名称 - 这种格式让我在使用gridviews时遇到了很多麻烦。尝试为'PropertyName'使用有意义的描述符:

<asp:GridView class="className" ID="gridviewID" runat="server" AutoGenerateColumns="false" BorderWidth="2px" ShowHeader="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                  <asp:Label ID="labelID" runat="server" size="10" Text='<%# Bind("PropertyName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>