I have the following code to load data from a list in sharepoint website to a custom web part.
private void Data_load()
{
DataTable dt = new DataTable();
string currentName = SPContext.Current.Web.CurrentUser.Name;
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Editor'/><Value Type='Person or Group'>" + currentName + "</Value></Eq></Where>";
using (SPSite site = new SPSite("http://spdev-6/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList lists = web.GetList("Lists/Advertisements");
SPListItemCollection items = lists.GetItems(query);
if (items.Count > 0)
{
//foreach(SPListItem item in items)
//{
// string decodeDescrip = Server.HtmlDecode( item["Details"].ToString());
// item["Details"] = decodeDescrip;
//}
dt = items.GetDataTable();
}
else
lbldata.Text = "No data to show";
GridViewD.DataSource = dt;
GridViewD.DataBind();
HttpContext.Current.Session["Advertisement"] = dt;
}
}
}
Now the issue is i am getting Htmlencode data in one of the columns. Now i have to remove it from grid view. So how it can be removed ?
You can modify the Column in the DataTable before assigning it to the Grid. Look here and here.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < row.Cells.Count - 1; i++)
{
if (row.Cells[i].Text == "0")
{
row.Cells[i].Text = "";
}
}
}
}
You could also change the value on DataBinding as described in this SO Answer.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < row.Cells.Count - 1; i++)
{
if (row.Cells[i].Text == "0")
{
row.Cells[i].Text = "";
}
}
}
}
Related
i have datagridview is bindingsurce table of database
datagridview load data from database
i wanna after get data from database edit row in datagredview using textboxs
i am using this code for Edit data
private void btn_Edit_Click(object sender, EventArgs e)
{
DataGridViewRow dr = dgvadministration.Rows[index_Row];
dr.Cells[2].Value = Convert.ToInt32(txt_Masseurs_Code.Text);
dr.Cells[4].Value = Convert.ToInt32(txt_Session_Type_Code.Text);
dr.Cells[6].Value = Convert.ToInt32(txt_C_Order_Masseurs.Text);
dr.Cells[7].Value = Convert.ToInt32(txt_Room_No.Text);
dr.Cells[8].Value = dtp_Session_Date.Text;
dr.Cells[9].Value = cbx_Session_done.Checked == true ? true : false;
dr.Cells[10].Value = txt_Session_Note.Text;
}
and get index_Row from dgvadministration_CellClick
private int index_Row;
private void dgvadministration_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
Clear_Session_Data();
index_Row = e.RowIndex;
session_Code_Serial = Convert.ToInt32(dgvadministration.SelectedRows[0].Cells[1].Value.ToString());
txt_Masseurs_Code.Text = dgvadministration.SelectedRows[0].Cells[2].Value?.ToString();
txt_Masseurs_Name.Text = dgvadministration.SelectedRows[0].Cells[3].Value?.ToString();
txt_Session_Type_Code.Text = dgvadministration.SelectedRows[0].Cells[4].Value?.ToString();
txt_Session_Type_Name.Text = dgvadministration.SelectedRows[0].Cells[5].Value?.ToString();
txt_C_Order_Masseurs.Text = dgvadministration.SelectedRows[0].Cells[6].Value?.ToString();
txt_Room_No.Text = dgvadministration.SelectedRows[0].Cells[7].Value?.ToString();
if (dgvadministration.SelectedRows[0].Cells[8].Value != null)
{
DateTime CoStartDate = (DateTime)dgvadministration.SelectedRows[0].Cells[8].Value;
dtp_Session_Date.Text = CoStartDate.ToShortDateString();
}
DataGridViewCheckBoxCell chk_Session_Done = dgvadministration.SelectedRows[0].Cells[9] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chk_Session_Done.Value) == true)
{
cbx_Session_done.Checked = true;
}
else
{
cbx_Session_done.Checked = false;
}
txt_Session_Note.Text = dgvadministration.SelectedRows[0].Cells[10].Value?.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
but dr.Cells[2].Value is null and al dr.Cells[X].Value
despite of Convert.ToInt32(txt_Masseurs_Code.Text); is not null and other textbox
Note before load data from database this code edit row is working when add Rows programmability using this code
DataTable dt = new DataTable();
dt.Columns.Add("Session_Code");
DataRow dr = null;
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr["Session_Code"] = i.ToString();
dt.Rows.Add(dr);
}
dgvadministration.DataSource = dt;
Why is this happening, and how do I Edit this Row ????
I have created a paginated grid view which contains check boxes for multiple selection.But while looping through the data-row in the grid-view only last pages row values only I am able to get.I know it's because of the pagination.
My code is below
protected void btnSaveItemMapping_Click(object sender, EventArgs e)
{
grdCustomers.AllowPaging = false;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
if (myCheckBox.Checked == true)
{
Label lblCustomerCode = (Label)gvrow.FindControl("lblCustomerCode");
//INSERT TO DATABSE
}
}
grdCustomers.AllowPaging = true
}
Keeping check box checked after changing page index like below code.
protected void grdCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
savechkdvls_cst();
grdCustomers.PageIndex = e.NewPageIndex;
if (Session["AllCustomers"] != null)
{
lstAllCustomers = (List<CustomerMaster>)Session["AllCustomers"];
}
grdCustomers.DataSource = lstAllCustomers;
grdCustomers.DataBind();
chkdvaluesp_cst();
}
private void chkdvaluesp_cst()
{
ArrayList usercontent = (ArrayList)Session["chkditems_customers"];
if (usercontent != null && usercontent.Count > 0)
{
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
int index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
if (usercontent.Contains(index))
{
CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkBoxBrandCustomers");
myCheckBox.Checked = true;
}
}
}
}
private void savechkdvls_cst()
{
ArrayList usercontent = new ArrayList();
int index = -1;
foreach (GridViewRow gvrow in grdCustomers.Rows)
{
index = Convert.ToInt32(grdCustomers.DataKeys[gvrow.RowIndex].Value);
bool result = ((CheckBox)gvrow.FindControl("chkBoxBrandCustomers")).Checked;
// Check in the Session
if (Session["chkditems_customers"] != null)
usercontent = (ArrayList)Session["chkditems_customers"];
if (result)
{
if (!usercontent.Contains(index))
usercontent.Add(index);
}
else
usercontent.Remove(index);
}
if (usercontent != null && usercontent.Count > 0)
Session["chkditems_customers"] = usercontent;
}
Any help will be appreciated.
I read text to the GridView and then move this text from the GridView to TextBoxes to edit. How can I replace the changed data from selection line and save to my text file?
this is my write file
private void button1_Click(object sender, EventArgs e)
{
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(#"WriteLines2.txt", true))
{
file.WriteLine(this.id.Text + "#" + this.judul.Text + "#" + this.isi.Text);
}
}
this is my gridview
System.IO.StreamReader file = new System.IO.StreamReader("WriteLines2.txt");
string[] columnnames = file.ReadLine().Split('#');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split('#');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
file.Close();
GridView.DataSource = dt;
this is my grid view move to textbox
private void GridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow dr = GridView.SelectedRows[0];
id.Text = dr.Cells["id"].Value.ToString();
// or simply use column name instead of index
//dr.Cells["id"].Value.ToString();
judul.Text = dr.Cells["judul"].Value.ToString();
isi.Text = dr.Cells["subjek"].Value.ToString();
//textBox4.Text = dr.Cells[3].Value.ToString();
GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
You should call the GridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; when you initialize your DataGridView and not during the CellContentClick. Then use the SelectionChanged Event instead of the CellContentClick Event and use CurrentCell.RowIndex for the index:
private void GridView_SelectionChanged(object sender, EventArgs e)
{
DataGridViewRow dr = GridView.Rows[GridView.CurrentCell.RowIndex];
id.Text = dr.Cells[0].Value.ToString();
judul.Text = dr.Cells[1].Value.ToString();
isi.Text = dr.Cells[2].Value.ToString();
}
Your buton1_Click adds a new values to the end of your file. I'm not sure if that is what you want.
i need to convert a field in gridview to a dropdownlist,
but i need to do this in codebehind, and I cannot add a templatefield in apsx(but it could be created at run time execution...)
I populate my grid with this code:
foreach (var item in response.Select(x => x.idMatriz).Distinct())
{
dr = dt.NewRow();
for (int i = 0; i < colunas; i++)
{
dr[i] = response.Where(x => x.Propriedade == dt.Columns[i].ToString() && x.idMatriz == item).Select(x => x.Valor).FirstOrDefault();
}
dt.Rows.Add(dr);
}
It works but i need this fileds be a dropdown....
any help?
It looks like all you need to do is dynamically create a template field and add it to the gridview.
var field = new TemplateField {HeaderText = col.ColumnName}
gridView.Columns.Add(field);
After that, on the row created event of the gridview create and wire up the dropdown.
public void DynamicGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
var grid = sender as GridView;
if (grid == null)
{
return;
}
for (var i = 0; i < grid.Columns.Count; i++)
{
var column = grid.Columns[i] as TemplateField;
if (column == null)
continue;
var cell = e.Row.Cells[i];
var dropdown = new DropDownList();
cell.Controls.Add(dropdown);
}
}
How can i get multiple selected rows in gridview using c# code & that selected rows i have to display in another form which also have gridview
public partial class WindowForm: Form
{
private DataTable dataTable = new DataTable();
//This will contain all the selected rows.
private List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
public WindowForm()
{
InitializeComponent();
dataTable .Columns.Add("Column1");
dataTable .Columns.Add("Column2");
dataTable .Columns.Add("Column3");
for (int i = 0; i < 30; i++)
{
dataTable .Rows.Add(i, "Row" + i.ToString(), "Item" + i.ToString());
}
dataGridView1.DataSource = dataTable ;
//This will select full row of a grid
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//This will allow multi selection
dataGridView1.MultiSelect = true;
dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
PerformSelection(dataGridView1, selectedRows);
}
void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (selectedRows.Contains(dataGridView1.CurrentRow))
{
selectedRows.Remove(dataGridView1.CurrentRow);
}
else
{
selectedRows.Add(dataGridView1.CurrentRow);
}
PerformSelection(this.dataGridView1, selectedRows);
}
private void PerformSelection(DataGridView dgv, List<DataGridViewRow> selectedRowsCollection)
{
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
if (selectedRowsCollection.Contains(dgvRow))
{
dgvRow.Selected = true;
}
else
{
dgvRow.Selected = false;
}
}
}
}