网格视图行数据绑定将数据放入特定单元格

时间:2013-11-29 15:46:50

标签: c# asp.net visual-studio-2012

我有3个表CustomerAddressMatching_Customer_Address

CustomerNameAddressCity。他们还有其他一些专栏,但我不需要它们。 NameCity列包含此类数据。

   NAME                  City
----------             -----------
John                    New York
----------             ----------
Karin                   Hamburg
---------              ----------
Jona                    Tokyo
---------              ----------
Martin

Matching_Customer_Address包含两个表之间匹配的ID。

我有一个网格视图,我将此GridView1绑定到数据表dt。 dt采用以下格式: 数据表第一列是“匹配”,其余列来自Address.City,行来自Customer.Name

由于Gridview1与dt绑定,因此网格视图就是这样。

Match | New York| Hamburg | Tokyo  |
----- | --------| ------- | ------ |
John  |         |         |        |
----- | ------- | ------- | ------ |
Karin |         |         |        |
----- | ------- | ------- | ------ |
Jona  |         |         |        |
----- | ------- | ------- | ------ |
Martin|         |         |        |
----- | ------- | ------- | ------ |

现在通过使用表Matching_Customer_Address,我想在GridView1单元格中放置一个字符“X”。我正在使用RowDataBound事件来执行此任务,但我不知道应该如何继续。

我正在尝试

 e.Row.Cells[].Text = "X"; 

我知道如何在这里访问Matching_Customer_Address表格但如果找到匹配项,我不知道如何在特定单元格中放置X.我是C#的新手。

以下是使用gridview绑定数据的代码。

DataTable dt = new DataTable();
SqlDataAdapter da_Customer, da_Address;
DataSet ds_Customer = new DataSet();
DataSet ds_Address = new DataSet();
SqlConnection con;
con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True");

da_Customer= new SqlDataAdapter("Select Name from Customer ", con);

da_Customer.Fill(ds_Customer, "Name");

da_Address = new SqlDataAdapter("Select City from Address ", con);

da_Address .Fill(ds_Address, "City");

int lengthofAddress = ds_Address.Tables[0].Select("City is not null").Length;
string[] getCols_City = new string[lengthofAddress];

int lengthofCustomer = ds_Customer.Tables[0].Select("Customer is not null").Length;
string[] getRows_Customer = new string[lengthofCustomer];


//added first column of dt.
        dt.Columns.Add(new DataColumn("Name", typeof(string)));

// This loop is getting rows from table City and adding them as column of dt.
        for (int x = 0; x < (lengthofAddress); x++)
        {

   string mystring = (ds_Address.Tables[0].Rows[x]["City"].ToString());

            getRows_Customer[x] = mystring;
            dt.Columns.Add(getRows_Customer[x]);

        }

// This loop is getting rows from table Customer and adding them as Rows of dt.
        for (int x = 0; x < (lengthofCustomer); x++)
        {

         getRows_Customer[x] = (ds_Customer.Tables[0].Rows[x]["Name"].ToString());


            dt.Rows.Add(getRows_Customer[x]);

        }


        GridView1.DataSource = dt;
        GridView1.DataBind();

Aspx代码在这里:

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderWidth="1px" CellPadding="4" Font-Names="Arial" Font-Size="Small" Width="100%"
BorderStyle="None" OnRowEditing="GridView1_RowEditing"  onrowdatabound="GridView1_RowDataBound" >

            <HeaderStyle CssClass="GridviewScrollHeader" /> 
            <RowStyle CssClass="GridviewScrollItem" /> 
            <PagerStyle CssClass="GridviewScrollPager" /> 
            </asp:GridView>

1 个答案:

答案 0 :(得分:1)

将逻辑放入您发布的数据表构建逻辑中,如下所示:

// This loop is getting rows from table Customer and adding them as Rows of dt.
for (int x = 0; x < (lengthofCustomer); x++)
{
    // Build the pieces of data for your row here

    // Name

    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here

    }
}

现在,当您将网格与数据表绑定时,您不需要处理RowDataBound事件,因为X已经在正确的单元格中。


更新:

要将值放入新数据表行,您需要创建一个新行,然后通过行的索引应用单元格值,如下所示:

DataRow row;

// Create new DataRow objects and add to DataTable.     
for(int i = 0; i < 10; i++)
{
    row = YourDataTable.NewRow();

    row["Name"] = theName;

    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here
        if(match)
        {
            row[y+1] = "X";
        }            
    }

    YourDataTable.Rows.Add(row);
}

更新2:

如果匹配列中每个人的行已经存在,那么循环遍历每一行,如下所示:

foreach(DataRow row in TheTable.Rows)
{
    // Loop through each city
    for (int y = 0; y < (lengthofAddress); y++)
    {
        // Determine if each city is a match or not,
        // if so then put "X" in that row's cell here
        if(match)
        {
            row[y+1] = "X";
        }            
    }
}