【发布时间】:2019-10-24 12:48:04
【问题描述】:
我有一个 DataGridView,它显示 SQLServer 数据库中的姓名、手机,并且具有 CheckBoxes。该按钮将向这些客户大量发送消息,这就是为什么我希望检查每个 CheckBox 以存储变量。问题是我发送的for循环中的消息没有发送出去。
要批量发送消息,此块用于字符串:
string bloque = "";
bloque += bloque + "ID1\tphoneNumber\tMessage\n";//client A
bloque += bloque + "ID2\tphoneNumber\tMessage\n";//client B
然后我想我可以通过循环来做不同的事情,这将自动为每个选中的 CheckBox 增加 ID,并将手机分配给循环,这样当我按下按钮时,它会将消息发送给所有选定的客户端.
目前,这就是我所有的代码:
public partial class Form1 : Form {
int contador = 0;
public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
allID();
//dtgId.ReadOnly = true;
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "Seleccione";
chk.Name = "check";
dtgId.Columns.Add(chk);
dtgId.AllowUserToAddRows = false;
}
public void allId(){
try{
string cadena = "cadena";
using (SqlConnection con = new SqlConnection(cadena)){
con.Open();
string query = "SELECT id, nombre, celular FROM clientes";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dtgId.DataSource = ds.Tables[0];
con.Close();
}
}
catch (SqlException ex){
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e){
List<string> celulares = new List<string>();
string cel;
foreach (DataGridViewRow row in dtgId.Rows){
if (row.Cells[1].Value.Equals(true)){
celulares.Add(row.Cells[0].Value.ToString());//columna donde estan los celulares
cel = row.Cells[0].Value.ToString();//variable con el celular
}
}
//Codigo para enviar SMS
string usuario= "usuario";
string clave = "clave";
string respuesta = "";
string text = txtTexto.Text;
string bloque = "";
//bloque += bloque + "ID1\tphoneNumber\tMessage\n";
I try to replace this with the for cycle
contador++;
for (int i = 0; i < cellphones.Count; i++){
bloque += bloque + "ID" + Convert.ToInt32(contador) + "\t" + cellphones[i] + "\t" + text + "\n";
}
}
}
【问题讨论】:
-
DGV 可能无法重新粉刷。当您更改 DGV 时,您需要将其设置为 null 以重新绘制:dtgId.DataSource = null; dtgId.DataSource = ds.Tables[0];
-
为什么将数据作为 http 请求传递,而不是直接使用 ADO.NET 将其保存到数据库中?
-
所以,我应该添加这一行:
dtgId.DataSource = null;? @jdwengspan> -
是的,以确保 DGV 得到更新。
标签: c# sql-server string winforms datagridview