使用C#填充GridView



是否可以使用C#函数填充Gridview表?对于Gridview,通常我会看到DataSourceSqlDataSource带有查询以检索数据。

我已经有一个List的对象,每个对象都有填充我的表的字段。每个对象都有9个字符串字段,每个字段应绑定到列。列表中的每个对象本质上代表表中的一行。c#文件中的代码会像这样吗?

for (int i = 0; i < credentials.Count; i++)
{
    for (int j = 0; j < 10; j++)
    {
        row1["ID"] = credentials[i].Id.ToString();
        row1["Windows ID"] = credentials[i].WindowsId;
        row1["First Name"] = credentials[i].FirstName.ToString();
        row1["Last Name"] = credentials[i].LastName.ToString();
        ...
        dt.Rows.Add(row1);
    }

}
gridRoles.DataSource = dt;
gridRoles.DataBind();

是的,您正在做的事情看起来应该有效,尽管我不确定您使用的是j的用途,因为它在上述代码中未使用。过去,我以编程方式填充DataGridView的方式是创建DataTable对象:

DataTable dt = new DataTable();

定义其列:

dt.Columns.Add(columnName);`

创建DataRow S:

DataRow row = dt.NewRow();
//fill row
row[columnName or index] = value; //repeat for needed values, could use a for loop and go through indexes

DataRow添加到DataTable

dt.Rows.Add(row);
dt.Rows.InsertAt(row, index); //if you want a specific position

最后,将DataGridViewDataSource设置为DataTable,就像您一样:

dgv.DataSource = dt;

注意:如果您没有DataGridView的列定义与DataRow S相同的列,则情况无法正常工作。

编辑:其他人一直在说您的数据应该是GridView的来源。我相信这会起作用,而且更快。我的解决方案将用于使用迭代创建新数据(例如您希望ID与GridView相匹配)。但是,为您的列表项目创建新模型并为此创建一些Getters可能会很方便模块化OOP。


在这种情况下,我将为您的GridView项目创建一个新模型(在这种情况下看起来像用户)。所以我在UWP上做了类似的事情,最终看起来像这样:

        ButtonRow = new List<ButtonModel>();
        int daysOnButtonRow = 365;
        TimeSpan additionalDay = new System.TimeSpan(1);
        for (int i = 0; i < daysOnButtonRow; i++)
        {
            ButtonRow.Add(new ButtonModel(DaysFromToday(i),   DaysFromToday(i + 1)));
        }

然后,按钮模型类包含每个按钮的特定参数和获取器,以获取我想在该按钮上显示的相关信息。

public class ButtonModel
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public ButtonModel(DateTime _startDate, DateTime _endDate)
    {
        StartDate = _startDate;
        EndDate = _endDate;
    }
    public string StartDateTruncatedName
    {
        get { return StartDate.ToString("dddd").Substring(0, 3).ToUpper(); }
    }
    public string StartDateDayNum
    {
        get { return StartDate.Day.ToString(); }
    }
    public string StartDateMonth
    {
        get { return StartDate.ToString("MMMM").ToUpper(); }
    }
}

这可以更干净,更模块化。ListView上的示例,但对于GridView应该类似。由于您已经使用了一个填充的列表,因此我建议使用" for-in"循环以及"迭代"循环"循环"。这将使之能做到这一点,以便每次将项目添加到列表中时,都会添加到GridView中。

希望这会有所帮助!:)

如果您已经有一个对象列表,则可以将其设置为GridView的数据源,无需转换或创建新的数据表。

gridRoles.DataSource = credentials;

为什么您不能使用对象datasource?在ObjectDatasource的属性中指定类和方法,您都已设置。将ObjectDataSource绑定到网格视图。

    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();

       try
        {
            using (SqlConnection con = new SqlConnection("Data Source = [SERVERNAME]; Initial Catalog = CustomerOrders; Integrated Security = true"))
            {
                String name = dropDownList.SelectedItem.Text;
                SqlDataAdapter cmd = new SqlDataAdapter("SELECT * FROM Customer INNER JOIN Orders ON Customer.CustomerID = Orders.ReferenceID WHERE Name = '" + name + "'", con);
                con.Open();
                DataTable dtbl = new DataTable();
                cmd.Fill(dtbl);
                gvPhoneBook.DataSource = dtbl;
                gvPhoneBook.DataBind();
            }
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.Message);
        }

最新更新