0

我有一个可编辑的 GridView,它通过电子邮件将其内容发送给所有公司。Gridview 的第一行包含公司名称(每行只有一个名称)。我想根据第一行中的公司名称将 Gridview 通过电子邮件发送到特定的电子邮件地址。例如 - 如果第一行等于 companyname1,则将电子邮件发送到 company one;如果第一行等于公司二,则将电子邮件发送给公司二。我尝试了以下操作:

//C# - 按钮后面的代码片段

foreach (GridView item in gvCompanies.Rows)
        {
            if (item.SelectedRow.Cells[1].Text == "company1")
            {
                txtEmailAddresses.Text = "company1@gmail.com";
            }
            else if (item.SelectedRow.Cells[1].Text == "company2")
            {
                txtEmailAddresses.Text = "company2.com";
            }

            else if (item.SelectedRow.Cells[1].Text == "company3")
            {
                txtEmailAddresses.Text = "company3@aol.com";
            }

            else if (item.SelectedRow.Cells[1].Text == "company4")
            {
                txtEmailAddresses.Text = "company@aol.com";
            }
        }

...有人可以就这里做错了什么提供一些指导吗?

4

1 回答 1

0

Try Something like this...

protected void gvCompanies_SelectedIndexChanged(object sender, EventArgs e)
{
 //for (int i = 0; i < gvCompanies.Rows.Count; i++)
 //       {
            if (gvCompanies.SelectedIndex == i)
            {
                String compName = gvCompanies.Rows[i].Cells[1].Text; //Gets the data cell value from the grid to string
                if(compName == "company1")
                {
                txtEmailAddresses.Text = "company1@gmail.com";
                }
                else if(compName == "company2")
                {
                txtEmailAddresses.Text = "company2@gmail.com";
                }
                ........and so on
            }
      // }
}

You need to set the folowing 2 properties of gridview.

1] AutoGenerateSelectButton="true" and
2] onselectedindexchanged="GridView1_SelectedIndexChanged".

于 2012-10-22T17:55:37.550 回答