Need a sharepoint 2010 webpart to have editable GridView - c#

I am a new sharepoint developer and trying to create webpart from VS2012 with sharepoint project workspace.
I have the below code in my workspace and want to show results in data grid view where i need last 2 columns to be editable. And then I need to submit the data to the sharepoint list.
protected override void CreateChildControls()
{
// Define the grid control that displays employee data in the Web Part.
grid = new DataGrid();
grid.Width = Unit.Percentage(100);
grid.GridLines = GridLines.Horizontal;
grid.HeaderStyle.CssClass = "ms-vh2";
grid.CellPadding = 2;
grid.BorderWidth = Unit.Pixel(5);
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
// Populate the grid control with data in the employee data file.
try
{
var dataEndPoint = new Uri("http://<ServerName>/_vti_bin/listdata.svc");
var dc = new TeamSiteDataContext(dataEndPoint);
dc.Credentials = CredentialCache
.DefaultNetworkCredentials;
----
When I add the gridview to the Controls collection - ie Controls.Add(grid), I do not have control about which columns are editable.

You can use SPGridView to achieve that, here is an example
http://code.msdn.microsoft.com/office/Ejemplos-sobre-cmo-usar-el-69cd5f16

Related

Dynamically created DevExpress DataGridView contains no data after setting DataSource

I have created a GridControl and GridView with the designer and set the DataSource and everything works fine. However when I create in code a new GridControl and a new GridView assigned to that GridControl and set the DataSource property correctly, then the GridView holds no data and RowCount returns 0. Any ideas?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
gridControl1.DataSource = StraddleDataHandler.Data;
GridControl g1 = new GridControl();
g1.DataSource = StraddleDataHandler.Data;
GridView v1 = new GridView(gridControl1);
Console.WriteLine(v1.RowCount);
}
}
Everything works fine when I set the DataSource property in my manually created grid control in designer, any ideas?
You need to set the BindingContext for the GridControl before it will generate.
g1.BindingContext = this.BindingContext;
The gridview is not being displayed with your code -- whatever was the default (main) grid view still is, even though you've created a second view.
As a result, DevExpress wisely (in my opinion) doesn't bother to render the grid view since it doesn't display.
If you make your view the mainview, I think you will see the rows appear.
GridView v1 = new GridView(gridControl1);
int before = v1.RowCount; // 0, per your example
gridControl1.MainView = v1;
int after = v1.RowCount; // now should have the correct rowcount
Just curious, but did you mean to do this instead?
GridView v1 = new GridView(g1); // instead of gridControl1?

C# 2015 - Gridview selected rows to RDLC report - Per page one row

I am working on C# 2015 windows application.
The basic idea is to get the records from the database and display in to the gridview. And print the selected records of grid view using RDLC report.
See the screenshot below for the basic idea.
Now, my print button code is as below:
private void btnPrint_Click(object sender, EventArgs e)
{
List<customer> lstCustomer = new List<customer>();
foreach (DataGridViewRow row in dgCustomers.SelectedRows)
{
customer c = new customer();
c.id = Convert.ToInt32(row.Cells[dgCustomers.Columns["id"].Index].Value);
c.firstName = row.Cells[dgCustomers.Columns["first_name"].Index].Value.ToString();
c.firstName = row.Cells[dgCustomers.Columns["last_name"].Index].Value.ToString();
lstCustomer.Add(c);
}
frmReport r = new frmReport();
r.Show();
ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
v.RefreshReport();
this.Hide();
}
See, I have created a List lstCustomer of class customer and added objects of the selected rows' cell values into it.
I have created a new form frmReport to display the report and added a reportviewer into it.
I have also taken an Rdlc report named rptBolt.rdlc and added it to the report viewer.
I have set the datasource of report with my list.
But now, I am not sure how to bind id, first name and last name into the report's text boxes.
P.S. I want separate page for each record.
I am not sure how to do it. Can anyone knows?
You can do like this,
In the frmReport, create one property called as ReportId and use as below.
/* Below Code goes to frmReport.cs */
//after the frmReport()
public int ReportId;
//In your forms where u have rdlc
frmReport r = new frmReport();
r.ReportId = GetTheSelectedRecordFromGridView();
r.Show();
ReportViewer v = r.Controls.Find("reportViewer1", true).FirstOrDefault() as ReportViewer;
v.LocalReport.ReportEmbeddedResource = "bolt.rptBolt.rdlc";
//Apply your ReportId to filter the record
//But it is good, to apply this filter to the original database call/query
lstCustomer = lstCustomer.Where(x=>x.Id==ReportId).ToList();
ReportDataSource dataset = new ReportDataSource("ReportDataSet1", lstCustomer);
v.LocalReport.DataSources.Clear();
v.LocalReport.DataSources.Add(dataset);
v.LocalReport.Refresh();
v.RefreshReport();
this.Hide();
private int GetTheSelectedRecordFromGridView()
{
//Write your logic, to get the selected Id;
}
Pass the reportId to frmReport, filter the record from database, build Report dataset with the desired fields, bind with reportviewer and display id, firstname and lastname fields.
Hope you got the logic / idea.

add the new row client has entered to data source on radgirdview

I am new to using the telerik tools for winforms and I was wondering if anyone could help me to find the best way so that the client can add a new row on the radgrid and for it to show this change on the data source.
so far I have the radgrid set up so that the client can add a new row. I just need to bind it to the data source.
private void radGridView1_Click(object sender, EventArgs e)
{
this.radGridView1.AllowEditRow = false;
this.radGridView1.AllowAddNewRow = true;
this.radGridView1.AllowDeleteRow = false;
this.radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}
Have a look at the UserAddedRow event for RadGridView. This is fired after the user added a new row to the grid. You could then add the new entries to a source list or data table.
List<Foo> _lSource = new List<Foo>();
DataTable _tSource = new DataTable();
private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
Foo foo = new Foo();
foo.Col1 = e.Row.Cells["col1"].Value.ToString();
foo.Col2 = e.Row.Cells["col2"].Value.ToString();
_lSource.Add(foo);
_tSource.Rows.Add(e.Row.Cells["col1"].Value.ToString(), e.Row.Cells["col2"].Value.ToString());
}
I added both possibilities in the same code snippet. Just choose one (list or table). I just created a random class to show you an example. You can create your own classes and name the properties as you want. Just note that the column (col1 and col2 in my example) must exist, otherwise you'll get a NullReferenceException. If you're using DataTable you have to add the columns once before adding rows.
_tSource.Columns.Add("col1");
_tSource.Columns.Add("col2");
I also recommend not to update the RadGridView properties on a click event of RadGridView. Because it is enough to set those properties once. Now you set them every time you click in your grid view. Either move them to the Load event of your form or set it directly in designer properties.
private void Form_Load(object sender, EventArgs e)
{
radGridView1.AllowEditRow = false;
radGridView1.AllowAddNewRow = true;
radGridView1.AllowDeleteRow = false;
radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}
RadGridView supports data binding to variety of data sources and CRUD operation will be handled for you automatically. Here you can find information on the supported data sources: Telerik UI for WinForms Documentation
And here is how to bind to a DataTable, where all CRUD operations work out of the box
RadGridView radGridView1 = new RadGridView();
this.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("value1", "value1");
table.Rows.Add("value2", "value2");
radGridView1.DataSource = table;
Here is also a design time tutorial.

Disabling button in DataGridViewButtonColumn

I pull results from Oracle database and store it in DataTable before setting it as source in DataGridView. The reason for this is I remove some columns before I show it to user. Using DataGridViewButtonColumn I have added column that has enables user to "Edit" specific row. My problem is I'm trying to disable that button based on value in that current row. Leave the rest of the row buttons enabled.
var table = OracleConnection.Results(myquery);
table.Columns.Remove("Comments");
var editButton = new DataGridViewButtonColumn;
editButton.Value = "Edit";
editButton.Text = "Edit";
editButton.UseColumnTextForButtonValue = true;
_dtgvResults.Source = table;
_dtgvResults.Columns.Add(editButton);
Take a look in OnRowDataBound event in your DataGridView
MS Reference

Programmatically adding bound Gridview to tabPanels

I want to add GridView programmatically in a TabPanel. In fact the tab panels themselves are created programmatically so I want to create a GridView for each TabPanel.
Here is the code to create the tab panels:
while (j < l.Count)
{
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText = testrub.RubricName.ToString();
GridView grid = new GridView();
grid.DataSource = element.GetElementByRubric(testrub.ID);
panel.Controls.Add(grid);
TabContainer1.Tabs.Add(panel);
j++;
}
As you can see the gridview that I want to create is databound. I would seriously appreciate some help. :)
Add this one line to your code
GridView grid = new GridView();
grid.DataSource = element.GetElementByRubric(testrub.ID);
grid.DataBind(); // <------ this new line

Categories