im editing languages know checkboxes in gridview i have string value english,spanish. while clicking edit button that checkboxlist items should be selected.
i want to get the gridview row column details and want to bind in one checkboxlist. i have binded all languages checkbox . i need to to make checkboxes as selected which are already in selected state.
gridview
<asp:TemplateField HeaderText="Languages">
<EditItemTemplate>
<asp:TextBox ID="txtLanguages" runat="server" Text='<%# Bind("Languages") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblLanguages" runat="server" Text='<%# Bind("Languages") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
private void bindLanguages()
{
string Languages = "English,Spanish";
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from tbl_Languages";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["LanguageName"].ToString();
item.Value = sdr["LanguageId"].ToString();
// item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
chkLanguages.Items.Add(item);
}
}
conn.Close();
}
}
}
public void Bindemployeedetails(string str)
{
DataTable dt = new DataTable();
string Languages = dt.Rows[0]["Languages"].ToString();//English,Hindi
string[] words = Languages.Split(',');
foreach (string word in words)
{
foreach (GridViewRow row in ChkLanguages.Items )
{
if (row.Cells[1].Text == word)
{
CheckBox chkRow = word.ToString();
chkRow.Checked = true;
}
}
}
}
Maybe try with split?
string[] words = Languages.Split(',');
foreach (string word in words)
{
foreach (GridViewRow row in gdvHealthProblem.Rows)
{
if (row.Cells[1].Text == word)
{
CheckBox chkRow = row.Cells[0].FindControl("chkTableHealthProblem") as CheckBox;
chkRow.Checked = true;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.